I am using max()
to find the maximum value in the list but the code below returns 4
though the max value is 90
.
List<Integer> list = new ArrayList<>(Arrays.asList(4,12,19,10,90,30,60,17,90));
System.out.println(list.stream().max(Integer::max).get());
Stream#max(Comparator)
takes a Comparator
. You'll want to use Integer#compare(int, int)
as that comparison function.
list.stream().max(Integer::compare).get()
You were providing Integer#max(int, int)
as an implementation of Comparator#compare(int, int)
. That method doesn't match the requirements of Comparator#compare
. Instead of returning a value indicating which is biggest, it returns the value of the biggest.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With