Lambda表达式中的方法引用与构造器引用

方法引用:

  1. 使用情境:当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用!
  2. 方法引用,本质上就是Lambda表达式,而Lambda表达式作为函数式接口的实例。所以方法引用,也是函数式接口的实例。
  3. 具体分为如下的三种情况:
    情况1 对象 :: 非静态方法
    情况2 类(对象) :: 静态方法
    情况3 类 :: 非静态方法
  4. 方法引用使用的要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表和返回值类型相同!(针对于情况1和情况2)
  • 情况一:对象 :: 实例方法
//Consumer中的void accept(T t)
        // Lambda写法
        Consumer con1 = str -> System.out.println(str);

        // 方法引用写法
        //Consumer中的void accept(T t)
        //PrintStream中的void println(T t)
        PrintStream ps = System.out;
        Consumer con2 = ps::println;
        Employee emp = new Employee(1001,"Tom",23,5600);

        // Lambda写法
        Supplier sup1 = () -> emp.getName();

        //Supplier中的T get()
        //Employee中的String getName()
        // 方法引用写法
        Supplier sup2 = emp::getName;
  • 情况二:类(对象) :: 静态方法
        //Comparator中的int compare(T t1,T t2)
        //Integer中的int compare(T t1,T t2)
        // 方法引用写法
        Comparator com2 = Integer::compare;
        // 原始写法
        Function func = new Function() {
            @Override
            public Long apply(Double d) {
                return Math.round(d);
            }
        };

        // Lambda写法
        Function func1 = d -> Math.round(d);

        //Function中的R apply(T t)
        //Math中的Long round(Double d)
        // 方法引用写法
        Function func2 = Math::round;
  • 情况三:类 :: 实例方法
        // Lambda写法
        Comparator com1 = (s1, s2) -> s1.compareTo(s2);

        // Comparator中的int comapre(T t1,T t2)
        // String中的int t1.compareTo(t2)
        // 方法引用写法
        Comparator com2 = String::compareTo;
        // Lambda写法
        BiPredicate pre1 = (s1, s2) -> s1.equals(s2);

        //BiPredicate中的boolean test(T t1, T t2);
        //String中的boolean t1.equals(t2)
        // 方法引用写法
        BiPredicate pre2 = String::equals;
        Employee employee = new Employee(1001, "Jerry", 23, 6000);

        // Lambda写法
        Function func1 = e -> e.getName();

        // Function中的R apply(T t)
        // Employee中的String getName();
        // 方法引用写法
        Function func2 = Employee::getName;

构造器引用:

  1. 和方法引用类似,函数式接口的抽象方法的形参列表和构造器的形参列表一致。
  2. 抽象方法的返回值类型即为构造器所属的类的类型
        //原始写法
        Supplier sup = new Supplier() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };

        // Lambda写法
        Supplier  sup1 = () -> new Employee();

        //构造器引用写法
        //Supplier中的T get()
        //Employee的空参构造器:Employee()
        Supplier  sup2 = Employee :: new;
        // Lambda写法
        Function func1 = id -> new Employee(id);

        //构造器引用写法
        //Function中的R apply(T t)
        Function func2 = Employee :: new;
        // Lambda写法
        BiFunction func1 = (id,name) -> new Employee(id,name);
        
        //构造器引用写法
        //BiFunction中的R apply(T t,U u)
        BiFunction func2 = Employee :: new;

数组引用

可以把数组看做是一个特殊的类,则写法与构造器引用一致。

        // Lambda写法
        Function func1 = length -> new String[length];

        //数组引用
        //Function中的R apply(T t)
        Function func2 = String[] :: new;

你可能感兴趣的:(Lambda表达式中的方法引用与构造器引用)