Use of Functional Interface in Java 8

2019-02-13 20:43发布

问题:

This is a follow up question from :: (double colon) operator in Java 8 in which Java allows you to refer to methods using :: operator.

Is it possible to provide some custom Functional Interface that I create and use it with :: operator? And how to do it?

回答1:

“Is it possible to provide some custom Functional Interface that I create and use it with :: operator? And how to do it?”

It is possible and it’s as easy as you could imagine: just create an interface with exactly one method. You don’t even need the @FunctionalInterface annotation; this annotation just documents your intention and helps detecting errors at compile time similar to @Override.

So maybe you already have created such interfaces in your pre-Java 8 projects…

class Foo {
    // nothing new:
    public interface FooFactory {
        Foo createFoo();
    }
    // new in Java 8:
    public static final FooFactory DEFAULT_FACTORY = Foo::new;
}


回答2:

How to provide custom functional interface implementation to use :: operator

public class TestingLambda {

    public static void main(String[] args) {
        int value1 = method(TestingLambda::customMethod1);
        int value2 = method(TestingLambda::customMethod2);

        System.out.println("Value from customMethod1: " + value1);
        System.out.println("Value from customMethod2: " + value2);
    } 

    public static int customMethod1(int arg){
        return arg + 1;
    }

    public static int customMethod2(int arg){
        return arg + 2;
    }

    public static int method(MyCustomInterface ob){
        return ob.apply(1);
    }

    @FunctionalInterface
    interface MyCustomInterface{
        int apply(int arg);
    }
}

Phase 1: Create a custom functional interface

I have created my own FunctionalInterface named MyCustomInterface and in Java 8 you have to declare the interface to be functional interface using @FunctionalInterface annotation. Now it has a single method taking int as param and returning int.

Phase 2: Create some methods confirming to that signature

Created two methods customMethod1 and customMethod2 which confirm to the signature of that custom interface.

Phase 3: Create a method that takes Functional Interface (MyCustomInterface) as argument

method takes MyCustomInterface in the argument.

And you are ready to go.

Phase 4: Use

In the main I have used the method and passed it the implementation of my custom methods.

method(TestingLambda::customMethod1); 


回答3:

Here you go

import java.util.Arrays;

class Sort {
    public int compareByLength(String s1, String s2) {
        return (s1.length() - s2.length());
    }
}

public class LambdaReferenceExample1 {
    public static void main(String[] javalatteLambda) {
        String[] str = {"one", "two", "3", "four", "five", "sixsix", 
            "sevennnnn", "eight"};
        Sort sort = new Sort();
        Arrays.sort(str, sort::compareByLength);

        for (String s : str) {
            System.out.println(s);
        }
    }
}