Java Collectors.groupingBy()---is List ordered?

China☆狼群 提交于 2019-12-30 02:45:29

问题


For the Collectors.groupingBy() that returns Map<K,List<T>> is it implied that the List<T> is in order that the stream is evaluated?

I see no explicit description of the ordering of the list, whereas the concurrent version explicitly states no ordering. If it weren't ordered somehow, I'd expect it to be a Collection though, and I don't see what other ordering it could possibly be, other than order received.

I'm hoping it's guaranteed that the last value in each list is the last value received for that group.


回答1:


The documentation for groupingBy() says:

Implementation Requirements:

This produces a result similar to:

groupingBy(classifier, toList());

The documentation for toList() says:

Returns:

a Collector which collects all the input elements into a List, in encounter order

So, to answer your question, as long as your stream has a defined encounter order, you're guaranteed to get ordered lists.


EDIT: As @Holger points out, groupingBy() would also have to respect encounter order to preserve toList()'s ordering constraint. The fact that it does is strongly implied in this note:

Implementation Note:

...If preservation of the order in which elements are presented to the downstream collector is not required, using groupingByConcurrent(Function, Collector) may offer better parallel performance.




回答2:


Unfortunately, this guarantee is not stated clearly.

However, the resulting Collector currently does not have the UNORDERED characteristic, so in fact, the resulting List is ordered.

The remaining question is, because there is no API contract disallowing it, could a future version (or an alternative implementation) add that characteristic and produce unordered lists? In practice, both OpenJDK and Oracle have been extremely unwilling to introduce such breaking changes even when there is strong justification for it.

Here, there is little justification to make such a change; I think it's safe to rely on this behavior.




回答3:


I did a real test, I init a ArrayList<TimeBased> with this order:

{"1", "2019-03-22 10:20:03", "1"},
{"2", "2019-03-22 10:30:03", "2"},
{"2", "2019-03-22 11:20:03", "3"},
{"1", "2019-03-22 11:20:15", "4"},
{"3", "2019-03-22 11:35:03", "5"},
{"2", "2019-03-22 12:20:03", "6"}

and groupingBy first and second column, but the result was:

id  birth                        number
1   Fri Mar 22 10:20:03 CST 2019 1
1   Fri Mar 22 11:20:15 CST 2019 4
2   Fri Mar 22 12:20:03 CST 2019 6
2   Fri Mar 22 11:20:03 CST 2019 3
2   Fri Mar 22 10:30:03 CST 2019 2
3   Fri Mar 22 11:35:03 CST 2019 5

so you see, the order is unexpected(date column order confused).

and after i do this(add LinkedList::new):

Map<Integer, Map<Date, List<TimeBased>>> grouped =
                timeBasedBeans.stream().collect(groupingBy(TimeBased::getId, groupingBy(TimeBased::getPeriod,
                        LinkedHashMap::new, toList())));

then the order is right:

id  birth                        number
1   Fri Mar 22 10:20:03 CST 2019 1
1   Fri Mar 22 11:20:15 CST 2019 4
2   Fri Mar 22 10:30:03 CST 2019 2
2   Fri Mar 22 11:20:03 CST 2019 3
2   Fri Mar 22 12:20:03 CST 2019 6
3   Fri Mar 22 11:35:03 CST 2019 5


来源:https://stackoverflow.com/questions/39172981/java-collectors-groupingby-is-list-ordered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!