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);
    }
}

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