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

11,910

Solution 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.

Solution 2

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

Solution 3

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.

Share:
11,910

Related videos on Youtube

Joel
Author by

Joel

Updated on June 04, 2022

Comments

  • Joel
    Joel almost 2 years

    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.

    • Brian Goetz
      Brian Goetz over 7 years
      Short answer: if you have an ordered stream (i.e., your source is a List, array, Iterator, etc) and you don't use an unordered collector (like the concurrent collectors), yes; elements will appear as if sequentially presented to the collector in encounter order. Note that this sometimes has a cost, and you don't care about this stability guarantee, in which case you could shed the order by including unordered() in your stream.