Java 8 stream’s .min() and .max(): why does this compile?

Note: this question originates from a dead link which was a previous SO question, but here goes…

See this code (note: I do know that this code won’t “work” and that Integer::compare should be used — I just extracted it from the linked question):

final ArrayList <Integer> list 
    = IntStream.rangeClosed(1, 20).boxed().collect(Collectors.toList());

System.out.println(list.stream().max(Integer::max).get());
System.out.println(list.stream().min(Integer::min).get());

According to the javadoc of .min() and .max(), the argument of both should be a Comparator. Yet here the method references are to static methods of the Integer class.

So, why does this compile at all?

5 Answers
5

Leave a Comment