Java 8 stream max() function argument type Comparator vs Comparable

前端 未结 4 1850
陌清茗
陌清茗 2021-01-01 16:41

I wrote some simple code like below. This class works fine without any errors.

public class Test {
    public static void main(String[] args) {
        List&         


        
4条回答
  •  天涯浪人
    2021-01-01 17:09

    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 and n-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. If Identifier refers to an instance method, then the implicit lambda expression has an extra parameter compared to if Identifier 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 implements Comparable interface - not Comparator.

    Integer::compareTo as an expression doesn't implement any interface. However, it can refer to/represent different functional types, one of which is Comparator.

    Comparator a = Integer::compareTo;
    BiFunction b = Integer::compareTo;
    ToIntBiFunction c = Integer::compareTo;
    

提交回复
热议问题