Lambda 方法引用

1.方法引用:若Lambda 体中的内容有方法已经实现了,我们可以使用“引用方法”(可以理解为方法引用是Lambda表达式的另外一种表现形式)

  方法引用主要有三种语法格式:

  ①  对象 :: 实例方法名

  ②  类 :: 静态方法名

  ③  类 :: 实例方法名

实例说明

//对象 :: 实例方法名
    @Test
    public void test1(){
        //Lambda表达式
        Consumer<String> con = (x) -> System.out.println(x);
        con.accept("hello Lambda");
        
        //方法引用
        PrintStream ps = System.out;
        Consumer<String> con1 = ps :: println;
        con1.accept("hello Lambda1");
        
        //简化一步为
        Consumer<String> con2 = System.out :: println;
        con1.accept("hello Lambda2");
    }
    
    //对象 :: 实例方法名
    @Test
    public void test2() {
        Employee emp = new Employee("张三", 18, 8888.88);
        
        //Lambda
        Supplier<String> sup = () -> emp.getName();
        String str = sup.get();
        System.out.println(str);
        
        //方法引用
        Supplier<Integer> sup2 = emp :: getAge;
        int age = sup2.get();
        System.out.println(age);
        
    }
    
    //类 :: 静态方法名
    @Test
    public void test3(){
        
        //Lambda
        Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
        int res = com.compare(2, 0);
        System.out.println(res);
        
        //方法引用
        Comparator<Integer> com2 = Integer :: compare;
        int res2 = com2.compare(1, 3);
        System.out.println(res2);
    }
    
    //类 :: 实例方法名
    @Test
    public void test4(){
        
        //Lambda
        BiPredicate<String, String> bp = (x, y) -> x.equals(y);
        boolean flag = bp.test("hello", "hello");
        System.out.println(flag);
        
        //方法引用
        BiPredicate<String, String> bp2 = String :: equals;
        boolean flag2 = bp2.test("hello", "world");
        System.out.println(flag2);
    }

注意: 

  ① Lambda 体中调用方法的参数列表与返回值类型,要与函数式接口抽象方法的函数列表和返回值类型保持一致!
  ② Lambda 参数列表中的第一参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用ClassName :: Method

2.构造器引用

  构造器引用语法格式:

  ClassName :: new

实例说明

 

//构造器引用 ClassName :: new
    @Test
    public void test5(){
        //Lambda 无参数构造方法
        Supplier<Employee> sup = () -> new Employee();
        Employee employee = sup.get();
        
        //构造器引用
        Supplier<Employee> sup2 = Employee :: new;
        Employee employee2 = sup2.get();
        
        //有一个参数构造方法
        Function<String, Employee> fun = (x) -> new Employee(x);
        Employee emp1 = fun.apply("张三");
        
        Function<String, Employee> fun2 = Employee :: new;
        Employee emp2 = fun2.apply("张三");
        
        //两个参数的构造方法
        BiFunction<String, Integer, Employee> bf = (x, y) -> new Employee(x, y);
        Employee emp3 = bf.apply("李四", 18);
        
        BiFunction<String, Integer, Employee> bf2 = Employee :: new;
        Employee emp4 = bf.apply("李四", 18);
        System.out.println(emp4.getName());
    }

 注意:需要调用的构造器的参数列表要与函数式接口中的抽象方法的参数列表保持一致!

3.数组引用

  数组引用语法格式:

  Type[] :: new;

实例说明

//数组引用 Type[] :: new;
    @Test
    public void test6() {
        Function<Integer, String[]> fu = (x) -> new String[x];
        String[] strs = fu.apply(10);
        System.out.println(strs.length);
        
        Function<Integer, String[]> fu2 = String[] :: new;
        String[] strs2 = fu2.apply(8);
        System.out.println(strs2.length);
    }

 Employee类

public class Employee {
    private String name;
    private int age;
    private double salary;
    
    public Employee() {
        
    }
    
    public Employee(String name) {
        this.name = name;
    }
    
    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public Employee(String name, int age, double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    
}

 

posted @ 2018-10-16 17:11  谋知  阅读(1814)  评论(0编辑  收藏  举报