Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 find max [duplicate]

Tags:

java

java-8

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());
like image 503
adam.kubi Avatar asked Dec 19 '14 16:12

adam.kubi


1 Answers

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.

like image 157
Sotirios Delimanolis Avatar answered Sep 21 '22 01:09

Sotirios Delimanolis