简体   繁体   中英

How is a Method reference compatible with functional interface with different implementing function names?

Here, a reference to the static method isPrime( ) is passed as the first argument to numTest( ). This works because isPrime is compatible with the IntPredicate functional interface. Thus, the expression MyIntPredicates::isPrime evaluates to a reference to an object in which isPrime( ) provides the implementation of test( ) in IntPredicate.

How can/does isPrime() provide an implementation of test() using a different reference/name other than test(). I guess this is the point of doing it in Java 8 gives more flexibility and possibilities. Can someone explain if this is new ? and how does it work this way ?

Thanks !

//Demonstrate a method reference for a static method.
//A functional interface for numeric predicates that operate
//on integer values.
interface IntPredicate {
    //the abstact to be implemented with something compatible
    boolean test(int n);
}

// This class defines three static methods that check an integer
// against some condition.
class MyIntPredicates {

    // A static method that returns true if a number is prime.
    static boolean isPrime(int n) {
        if (n < 2)
            return false;
        for (int i = 2; i <= n / i; i++) {
            if ((n % i) == 0)
                return false;
        }
        return true;
    }

    // A static method that returns true if a number is even.
    static boolean isEven(int n) {
        return (n % 2) == 0;
    }

    // A static method that returns true if a number is positive.
    static boolean isPositive(int n) {
        return n > 0;
    }
}

public class MethodRefDemo {
    // This method has a functional interface as the type of its
    // first parameter. Thus, it can be passed a reference to any
    // instance of that interface, including one created by a
    // method reference.
    static boolean numTest(IntPredicate p, int v) {
        return p.test(v);
    }


    public static void main(String args[]) {
        boolean result;
        // Here, a method reference to isPrime is passed to numTest().
        result = numTest(MyIntPredicates::isPrime, 17);
        if (result)
            System.out.println("17 is prime.");
        // Next, a method reference to isEven is used.
        result = numTest(MyIntPredicates::isEven, 12);
        if (result)
            System.out.println("12 is even.");
        // Now, a method reference to isPositive is passed.
        result = numTest(MyIntPredicates::isPositive, 11);
        if (result)
            System.out.println("11 is positive.");
    }
}

This is an example of lambdas at work. It is new in java 8 and it basically allows the runtime to create an instance of the functional interface for you with the implementation you define.

Brian Goetz has written a doc on how lambdas (and method references) work at compile and runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM