JDK自带的函数接口

位置:java.util.function.BiConsumer

test1

接口分类

消费型

  • Consumer<T>

    1void accept(T t);
    
  • BiConsumer<T, U>

    1void accept(T t, U u);   // 增加一种入参类型
    

供给型

  • Supplier<T>

    1/**
    2 * Gets a result.
    3 *
    4 * @return a result
    5 */
    6T get();
    

函数型

  • Function<T, R>

  • UnaryOperator<T>

  • BiFunction<T, U, R>

  • BinaryOperator<T>

  • ToIntFunction<T>

  • ToLongFunction<T>

  • ToDoubleFunction

  • IntFunction<R>

  • LongFunction<R>

  • DoubleFunction<R>

断言型

  • Predicate<T>

    1/**
    2  * Evaluates this predicate on the given argument.
    3  *
    4  * @param t the input argument
    5  * @return {@code true} if the input argument matches the predicate,
    6  * otherwise {@code false}
    7  */
    8boolean test(T t);
    

方法引入

  • 静态方法引入

    ​ 类名::(静态)方法名称

  • 对象方法引入

    ​ 类名::实例方法名称

  • 实例方法引入

    1. new 对象
    1. 对象::方法名称
    
  • 构造函数引入

    ​ 类名::new

方法引入的方法参数列表、返回类型必须要与函数接口的参数列表和返回值保持一致

静态方法引入

 1@FunctionalInterface
 2public interface MyFunctionalInterface {
 3  void get();
 4  default void add(){}
 5  String toString();
 6}
 7
 8public class Test10 {
 9  public static void main(String[] args) {
10    MyFunctionalInterface myFunctionalInterface = new MyFunctionalInterface() {
11      @Override
12      public void get() {
13        System.out.println("get() method");
14      }
15    };
16
17    myFunctionalInterface.get();
18
19    // 静态方法引入
20    MyFunctionalInterface myFunctionalInterface1 = Test10::staticGet;
21    myFunctionalInterface1.get();
22  }
23
24  // 该方法的参数和返回类型和MyFunctionalInterface函数接口的参数列表和返回类型是一致的
25  public static void staticGet() {
26    System.out.println("static get method");
27  }
28}

对象方法引入

 1public class Test14 {
 2  public static void main(String[] args) {
 3    MyFunctionalInterface04 myFunctionalInterface04 = new MyFunctionalInterface04() {
 4      @Override
 5      public String get(Test14 test14) {
 6        return test14.objGet();
 7      }
 8    };
 9
10    System.out.println(myFunctionalInterface04.get(new Test14()));
11
12    // lambda 表达式监护
13    myFunctionalInterface04 = test14 -> test14.objGet();
14    System.out.println(myFunctionalInterface04.get(new Test14()));
15
16    // 对象构造方法引入
17    // 以往情况下由于objGet方法并不是static方法不能使用Test14::objGet方式
18    // 但是由于函数接口中的get方法需要传入一个new Test14() 对象
19    // 直接简化成了Test14::objGet方式
20    myFunctionalInterface04 = Test14::objGet;
21    System.out.println(myFunctionalInterface04.get(new Test14()));
22  }
23
24  public String objGet(){
25    return "object get method exec.";
26  }
27}

实例方法引入

 1public class Test12 {
 2  public static void main(String[] args) {
 3    Test12 test12 = new Test12();
 4    MyFunctionalInterface02 myFunctionalInterface = () -> test12.objectGet();
 5    System.out.println(myFunctionalInterface.get());
 6
 7    // lambda表达式写法
 8    MyFunctionalInterface02 myFunctionalInterface02 = test12::objectGet;
 9    System.out.println(myFunctionalInterface.get());
10  }
11
12  public String objectGet() {
13    return "object get method.";
14  }
15}

构造函数方法引入

 1public class Test13 {
 2  public static void main(String[] args) {
 3    MyFunctionalInterface03 myFunctionalInterface03 = () -> new MessageEntity();
 4    System.out.println(myFunctionalInterface03.get());
 5
 6    // lambda 表达式
 7    MyFunctionalInterface03 myFunctionalInterface = MessageEntity::new;
 8    System.out.println(myFunctionalInterface.get());
 9  }
10}
— END —