Java 8 stream max() function argument type Comparator vs Comparable
I can relate to your confusion.
We've got a Comparator
's method which declares two parameters
int compare(T o1, T o2);
and we've got an Integer
's method which takes one parameter
int compareTo(Integer anotherInteger)
How on earth does Integer::compareTo
get resolved to a Comparator
instance?
When a method reference points to an instance method, the parser can look for methods with arity n-1
(n
is the expected number of parameters).
Here's an excerpt from the JLS on how applicable methods are identified. I will drop the first part about parsing the expression preceding the ::
token.
Second, given a targeted function type with
n
parameters, a set of potentially applicable methods is identified:If the method reference expression has the form
ReferenceType :: [TypeArguments] Identifier
, then the potentially applicable methods are:
the member methods of the type to search that would be potentially applicable (§15.12.2.1) for a method invocation which names Identifier, has arity n, has type arguments TypeArguments, and appears in the same class as the method reference expression; plus
the member methods of the type to search that would be potentially applicable for a method invocation which names
Identifier
, has arity n-1, has type arguments TypeArguments, and appears in the same class as the method reference expression.Two different arities,
n
andn-1
, are considered, to account for the possibility that this form refers to either a static method or an instance method....
A method reference expression of the form
ReferenceType :: [TypeArguments] Identifier
can be interpreted in different ways. IfIdentifier
refers to an instance method, then the implicit lambda expression has an extra parameter compared to ifIdentifier
refers to a static method.https://docs.oracle.com/javase/specs/jls/se12/html/jls-15.html#jls-15.13.1
If we were to write an implicit lambda expression from that method reference, the first (implicit) parameter would be an instance to call the method on, the second (explicit) parameter would be an argument to pass in the method.
(implicitParam, anotherInteger) -> implicitParam.compareTo(anotherInteger)
Note that a method reference differs from a lambda expression, even though the former can be easily transformed into the latter. A lambda expression needs to be desugared into a new method, while a method reference usually requires only loading a corresponding constant method handle.
Integer::compareTo
implementsComparable
interface - notComparator
.
Integer::compareTo
as an expression doesn't implement any interface. However, it can refer to/represent different functional types, one of which is Comparator<Integer>
.
Comparator<Integer> a = Integer::compareTo;
BiFunction<Integer, Integer, Integer> b = Integer::compareTo;
ToIntBiFunction<Integer, Integer> c = Integer::compareTo;
int value = intList.stream().max(Integer::compareTo).get();
The above snippet of code is logically equivalent to the following:
int value = intList.stream().max((a, b) -> a.compareTo(b)).get();
Which is also logically equivalent to the following:
int value = intList.stream().max(new Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return a.compareTo(b);
}
}).get();
Comparator
is a functional interface and can be used as a lambda or method reference, which is why your code compiles and executes successfully.
I recommend reading Oracle's tutorial on Method References (they use an example where two objects are compared) as well as the Java Language Specification on §15.13. Method Reference Expressions to understand why this works.