0

I've noticed something strange about Collections.groupingBy. I know that the elements are not ordered but is there someone who can explain me the following scenarios:

public class A{
    Integer index;
    // Constructor
}

1. List<A> listOfA = Arrays.asList(new A(3), new A(1), new A(5));
    System.out.println(listOfA.stream().collect(Collections.groupingBy(A::getIndex));
=> Will print 1, 3, 5 in an ordered way

2. List<A> listOfA = Arrays.asList(new A(1), new A(31), new A(5), new A(32));
    System.out.println(listOfA.stream().collect(Collections.groupingBy(A::getIndex));
=> Will print 32, 1, 3, 5, 31 in an unordered way

Why 32 was added before 1 ?

pcCC28
  • 179
  • 1
  • 7
  • 20
  • 2
    The elements aren't unordered. The ordering is just unspecified. – Andy Turner Sep 11 '19 at 19:32
  • Ok, but if keys are less than 31 then the elements are ordered, if not any value greater than 31 will be added before the smallest key value.. for my example before 1 – pcCC28 Sep 11 '19 at 19:34
  • 2
    @VishwaRatna this is nothing to do with lazy evaluation... – Ousmane D. Sep 11 '19 at 19:38
  • Edited, my bad Hadi J – pcCC28 Sep 11 '19 at 19:42
  • Aomine This question is not duplicated – pcCC28 Sep 11 '19 at 19:44
  • If you are asking just of curiosity(and not looking for a way to get them sorted) then think how HashMap works - the ordering is far from unspecified. – Lesiak Sep 11 '19 at 20:06
  • This has nothing to do with `Stream`s and everything to do with which `Map` implementation the grouping operation uses. This is explained in the accepted answer to the linked duplicate. In the current implementation, the grouping operation uses a `HashMap` unless otherwise specified; the order of a `HashMap` is _unspecified_ (contractually). – Slaw Sep 11 '19 at 20:06
  • Voted to reopen - in general the order of HashMap is not specified, but here we have a special case – Lesiak Sep 11 '19 at 20:10
  • 1
    This question might be a better duplicate: [Is the order of values retrieved from a HashMap the insertion order](https://stackoverflow.com/questions/2144776/is-the-order-of-values-retrieved-from-a-hashmap-the-insertion-order). – Slaw Sep 11 '19 at 20:17
  • @Slaw I totally agree – Lesiak Sep 11 '19 at 20:19
  • 1
    @Slaw that other question is a great addition, however, since the `groupingBy` collector doesn’t even specify that the result will be a `HashMap`, the primary answer is Stream specific, i.e. that the ordering behavior is entirely unspecified. In principle, it would be legal for an implementation to return a map that does maintain the insertion order or provides a natural order for comparable keys. So an answer describing the behavior of `HashMap` is only sufficient when also explicitly saying that the particular `groupingBy` implementation does happen to return a `HashMap`. – Holger Sep 12 '19 at 13:57
  • @pcCC28 By the way, it’s `Collectors.groupingBy` instead of `Collections.groupingBy`. Further, there’s a `)` missing. – Holger Sep 12 '19 at 14:03

0 Answers0