Java8之方法引用与构造器引用

package com.alisa.java8;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Comparator;
import java.util.function.*;

/*
* 一、方法引用:当要传递给Lambda体的操作已经有实现的方法了,可以使用方法引用
* (可以将方法引用理解为Lambda表达式的另外一种表现形式)
* 1、对象的引用::实例方法名
* 2、类名::静态方法名
* 3、类名::实例方法名
*注意:
* (1)方法引用所引用的方法的参数列表与返回值类型,
* 需要与函数式接口中抽象方法的参数列表和返回值类型保持一致
*(2)若Lambda的参数列表的第一个参数,是实例方法的调用者,
* 第二个参数(或无参)是实例方法的参数时,格式:ClassName::MethodName
*
* 二、构造器引用:构造器的参数列表,需要与函数式接口中参数列表保持一致
* 类名::new
*
* 三、数组引用
* 类型[]::new
* */
@RunWith(SpringRunner.class)
@SpringBootTest
public class MethodRefTest {
    //对象的引用::实例方法名
    @Test
    public void test1(){
        Consumer<String> com1 = (str) -> System.out.println(str);
        com1.accept("Lambda");
        Consumer<String> com2 = System.out::println;
        com2.accept("Hello Lambda");
    }

    @Test
    public void test2(){
        Employee emp = new Employee(101,"张三",18,10000);
        Supplier<String> sup1 = () -> emp.getName();
        System.out.println(sup1.get());
        Supplier<Integer> sup2 = emp::getAge;
        System.out.println(sup2.get());
    }

    //类名::静态方法名
    @Test
    public void test3(){
        BiFunction<Double,Double,Double> bf1 = (x,y) -> Math.max(x,y);
        System.out.println(bf1.apply(23.0,12.0));
        BiFunction<Double,Double,Double> bf2 = Math::max;
        System.out.println(bf2.apply(1.0,2.0));
    }

    @Test
    public void test4(){
        Comparator<Integer> com1 = (x,y) -> Integer.compare(x,y);
        System.out.println(com1.compare(2,4));
        Comparator<Integer> com2 = Integer::compare;
        System.out.println(com2.compare(4,5));
    }

    //类名::实例方法名
    @Test
    public void test5(){
        BiPredicate<String,String> bp1 = (x,y) -> x.equals(y);
        System.out.println(bp1.test("Lambda","lambda"));
        BiPredicate<String,String> bp2 = String::equals;
        System.out.println(bp2.test("LAMBDA","lambda"));
    }

    @Test
    public void test6(){
        Function<Employee,String> fun1 = (e) -> e.show();
        System.out.println(fun1.apply(new Employee()));
        Function<Employee,String> fun2 = Employee::show;
        System.out.println(fun2.apply(new Employee()));
    }

    //构造器引用
    @Test
    public void test7(){
        Function<String,Employee> fun1 = (str) -> new Employee(str);
        System.out.println(fun1.apply("alisa"));
        Function<String,Employee> fun2 = Employee::new;
        System.out.println(fun2.apply("Lambda"));
    }

    @Test
    public void test8(){
        Supplier<Employee> sup1 = () -> new Employee();
        System.out.println(sup1.get());
        Supplier<Employee> sup2 = Employee::new;
        System.out.println(sup2.get());
    }

    //数组引用
    @Test
    public void test9(){
        Function<Integer,String[]> fun1 = (x) -> new String[x];
        String[] str1 = fun1.apply(5);
        System.out.println(str1.length);
        Function<Integer,String[]> fun2 = String[]::new;
        String[] str2 = fun2.apply(8);
        System.out.println(str2.length);
    }

    @Test
    public void test10(){
        Function<Integer,Employee[]> fun1 = (x) -> new Employee[x];
        Employee[] emps1 = fun1.apply(10);
        System.out.println(emps1.length);
        Function<Integer,Employee[]> fun2 = Employee[]::new;
        Employee[] emps2 = fun2.apply(20);
        System.out.println(emps2.length);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章