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

Joel :

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.

shmosel :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=448245&siteId=1