I am learning 1z0-809: Java SE 8 Programmer II using Enthuware layouts.
Meet this question.
List<Integer> ls = Arrays.asList(3,4,6,9,2,5,7); System.out.println(ls.stream().reduce(Integer.MIN_VALUE, (a, b)->a>b?a:b)); //1 System.out.println(ls.stream().max(Integer::max).get()); //2 System.out.println(ls.stream().max(Integer::compare).get()); //3 System.out.println(ls.stream().max((a, b)->a>b?a:b)); //4
Which of the above statements prints 9?
Answer
1 and 3
But there is something else. I do not understand why
System.out.println(ls.stream().max(Integer::max).get());
I tried debugging it using peek
, but that doesn't help me understand.
I tried sorting ls
using Integer::max
and Integer::compare
ls.sort(Integer::max); // [3, 4, 6, 9, 2, 5, 7] ls.sort(Integer::compare); // [2, 3, 4, 5, 6, 7, 9]
Of course, I get the fact that Integer::max
not a comparator, so it has the same signature. For me, max
should be 7
in the first case, since this is the last element, for example, when I sorted using Ìnteger::compare
Can someone break this into something simple?