This is unbelievably confusing.
We are trying use Integer::max
as a comparator. Since all the numbers in the question are positive, the answer is always interpreted as meaning that the first argument of compare(a, b)
is "greater" than the second.
One quirk of Collections.sort
is that when you have a list [a, b]
, the method is programmed in such a way that it is compare(b, a)
that is called, rather than compare(a, b)
(which would seem more sensible). Hence if compare
returns a positive number, it looks like b > a
, so the list does not need sorting.
That is why list.sort
does nothing.
However it just happens that Stream.max
is programmed the other way around, i.e. the first comparison is compare(3, 4)
, so it looks like 3
is the maximum.