Java8 new features - Functional Interface

Functional Interface

Functional Interface (Functional Interface) is one and only one abstract method, but there may be a plurality of non-abstract methods of the interface.

Interface function can be implicitly converted to lambda expressions.

Lambda expressions and method reference (in fact, can also be considered Lambda expressions) on.
The interface defines a function as follows:

@FunctionalInterface
interface GreetingService {
    void sayMessage(String message);
}

Then you can use Lambda expressions to represent an implementation of this interface (Note: JAVA is generally anonymous class implemented before 8):

GreetingService greetService1 = message -> System.out.println("Hello " + message);

Functional friendly interface to existing support lambda functions.
Before JDK 1.8 has a function interface:

  • java.lang.Runnable
  • java.util.concurrent.Callable
  • java.security.PrivilegedAction
  • java.util.Comparator
  • java.io.FileFilter
  • java.nio.file.PathMatcher
  • java.lang.reflect.InvocationHandler
  • java.beans.PropertyChangeListener
  • java.awt.event.ActionListener
  • javax.swing.event.ChangeListener
    the JDK 1.8 newly added function interface:
  • java.util.function

java.util.function It contains many classes, Java supports programming for the function, the packet interface functions are:

  • BiConsumer <T, U> represents an operation that takes two input parameters, and returns no result
  • BiFunction <T, U, R> represents a method accepts two input parameters, and returns a result
  • BinaryOperator represents an operation applied to two of the same type on the operator, and returns the result of the operator of the same type
  • BiPredicate <T, U> boolean value representing the two parameters of a method of
  • BooleanSupplier boolean value represents the result of the provider
  • Consumer represents accepts an operation input parameter and a non-return
  • DoubleBinaryOperator represents two double values ​​applied to the operation of the operator, and returns the results of a double value.
  • DoubleConsumer represents a double operating parameter value acceptance and returns the result.
  • Representative DoubleFunction method of receiving a double value of the parameter, and returns the result
  • On behalf of DoublePredicate a boolean value method has double value of the parameter
  • DoubleSupplier configuration represents a double value provider
  • DoubleToIntFunction accepts a double input type, returns a result of type int.
  • DoubleToLongFunction accepts a double input type, returns a result of type long
  • DoubleUnaryOperator accepts a parameter of the same type double, the return type is also double.
  • Function <T, R> accepts an input parameter and returns a result.
  • IntBinaryOperator accepts two parameters the same type int, also the type of return value to int.
  • IntConsumer requires an int input parameters, no return value.
  • IntFunction requires an int input and returns a result.
  • IntPredicate: accepting an int input parameter and returns a Boolean value of the result.
  • IntSupplier no parameters and returns a result of type int.
  • IntToDoubleFunction requires an int input, returns a result of type double.
  • IntToLongFunction requires an int input, returns a result of type long.
  • IntUnaryOperator accepts a parameter of the same type int, also the type of return value to int.
  • LongBinaryOperator accepts two parameters the same type of long, return type is also a long.
  • LongConsumer accept a long type input parameters, no return value.
  • LongFunction accepts a long type input parameters and returns a result.
  • LongPredicateR a long accepts input and returns a Boolean result type.
  • LongSupplier no parameters and returns a result value type long.
  • LongToDoubleFunction input accepts a long type, returns a result of type double.
  • LongToIntFunction input accepts a long type, returns a result of type int.
  • LongUnaryOperator accepts a parameter of the same type long, return type is also a long.
  • ObjDoubleConsumer accepts an object type and a double input type parameters, no return value.
  • ObjIntConsumer accepts an object type and an input parameter of type int, no return value.
  • ObjLongConsumer accepts an object type and a long type of input parameters, no return value.
  • Predicate accepts an input parameter and returns a boolean result.
  • Supplier no parameters and returns a result.
  • ToDoubleBiFunction <T, U> accepts two input parameters and returns a result of type double
  • ToDoubleFunction accepts an input parameter and returns a result of type double
  • ToIntBiFunction <T, U> accepts two input parameters and returns a result of type int.
  • ToIntFunction accepts an input parameter and returns a result of type int.
  • ToLongBiFunction <T, U> accepts two input parameters and returns a result of type long.
  • ToLongFunction accepts an input parameter and returns a result of type long.
  • UnaryOperator accepts a parameter of type T, also the type of return value T.

Examples of functional interfaces

Predicate interface is an interface function which accepts one input parameter T, returns a boolean result.

The interface comprises a plurality of default methods to be combined into other complex Predicate logic (such as: and, or, not).

This interface is used to test an object to true or false.

We can learn to use the function interface Predicate by the following examples (Java8Tester.java):

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
 
public class Java8Tester {
   public static void main(String args[]) {
      List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
 
      // Predicate<Integer> predicate = n -> true
      // n 是一个参数传递到 Predicate 接口的 test 方法
      // n 如果存在则 test 方法返回 true
 
      System.out.println("输出所有数据:");
 
      // 传递参数 n
      eval(list, n->true);
 
      // Predicate<Integer> predicate1 = n -> n%2 == 0
      // n 是一个参数传递到 Predicate 接口的 test 方法
      // 如果 n%2 为 0 test 方法返回 true
 
      System.out.println("输出所有偶数:");
      eval(list, n-> n%2 == 0 );
 
      // Predicate<Integer> predicate2 = n -> n > 3
      // n 是一个参数传递到 Predicate 接口的 test 方法
      // 如果 n 大于 3 test 方法返回 true
 
      System.out.println("输出大于 3 的所有数字:");
      eval(list, n-> n > 3 );
   }
 
   public static void eval(List<Integer> list, Predicate<Integer> predicate) {
      for(Integer n: list) {
         if(predicate.test(n)) {
            System.out.println(n + " ");
         }
      }
   }
}

The implementation of the above script, output is:

$ javac Java8Tester.java
$ java Java8Tester
输出所有数据:
1
2
3
4
5
6
7
8
9
输出所有偶数:
2
4
6
8
输出大于 3 的所有数字:
4
5
6
7
8
9

Other new features (Update)

Java8 new features -Lambad expression
Java8 new feature - a reference method
Java8 new features - the default method

Published 26 original articles · won praise 6 · views 2941

Guess you like

Origin blog.csdn.net/weixin_45676630/article/details/104931158