java8 function interface

包 java.util.function

A functional interface provides a target type for lambda expressions and method references, that is, the type of a lambda expression is a functional interface, which may seem a little strange, but in fact it is an effective way to ensure backward compatibility of the Java language.
Each functional interface contains only one abstract method, such as:

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("In another thread");
            }
        });
        thread.start(); 
        
        Predict<String> predict = String::isEmpty;

There are many functional interfaces with similar names in java.util.function, but after careful analysis, you will find that they have the following naming rules:

  • 1 Four basic interface Function
  • 2 The interface determined by the three generic parameters of double, int and long corresponding to the four basic interfaces. And the abstract methods prefixed with Bi are extended from unary to binary functions and
    there are UnaryOperator (extends Function) and BinaryOperator (extends BiFunction).
  • 3 If the interface method returns a basic type and the parameter is a generic type, add To in front of the basic type, such as ToDoubleBiFunction,ToDoubleFunction,ToIntBiFunction,ToIntFunction,ToLongBiFunction,ToLongFunction.
  • 4 If the generic parameter is a basic type without prefix To, such as Long/Int/DoubleFunction, Long/Int/DoubleConsumer, Long/Int/DoublePredicate, Long/Int/DoubleSupplier.
  • 5 Three special Consumer interface functions ObjDoubleConsumer, ObjIntConsumer, ObjLongConsumer that receive two parameters,
  • 6 There is also a special type of Funtion interface function TypeToTypeFunction, where Type is the basic type of Long, Double, and Int. That is, it takes one basic type parameter and returns another basic type parameter.
Three rules for interface functions:
  • 1 A functional interface can have only one abstract method.
  • 2 An abstract method that is a public method in the Object class is not considered a single abstract method.
  • 3 Functional interfaces can have default methods and static methods.

Any interface that satisfies a single abstract method is automatically considered a functional interface. Including traditional interfaces such as Runnable, Callable, Comparator, etc. The annotation @FunctionalInterface was introduced in java8. When violating the provisions of the functional interface, the compiler will report an error. It should be noted that even if the annotation is omitted, the function of the functional interface is also valid. The annotation is only for the compiler to enforce the check, which is similar to the function of @Override . Also, since default methods are not abstract, custom methods can be added to the functional interface at will. Another important point to keep in mind is that if an interface declares an abstract method that overrides a public method in java.lang.Object, that method also doesn't count towards the interface's abstract method count, since any implementation of the interface will have a method from Methods of java.lang.Object.

custom function interface

There are Function and BiFunction in the java.util.function package, but there is no TriFunction. If we need such a functional interface, what should we do?
Don't worry, we can customize one. The code is as follows

        @FunctionalInterface
        public interface TriFunction<T, U, V, R> {
        
            R apply(T t, U u, V v);
        }
        
        Integer doSum(String s1, String s2) {
            return Integer.parseInt(s1) + Integer.parseInt(s2);
        }
        
        public void test() {
            
            TriFunction<Sum, String, String, Integer> t2 = Sum::doSum;
            
        }
Closing remarks:

The design decision to make lambda expressions a functional interface type helps java backwards compatibility. In some cases it is reasonable to create your own functional interface, but
you should do so with care and only consider custom functional interfaces if your design requires a high level of abstraction or if existing class libraries are not sufficient.

reference

java.util.function interface documentation
java8 idioms - functional interface
java functional interface tutorial
java8 functional programming

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325292158&siteId=291194637