JDK8 - 构造器引用和数组引用

1. 构造器引用

demo1:

public class ConstructorRefTest {
    public static void main(String[] args) {
        // lambda
        Supplier<Person> supplier = ()-> new Person();
        // 构造器引用
        Supplier<Person> supplier2 = Person::new;
    }
}

 

demo2:

public class ConstructorRefTest {
    public static void main(String[] args) {
        // lambda
        Function<String, Person> func1 = str -> new Person(str);
        System.out.println(func1.apply("master shi"));

        // 构造器引用
        Function<String, Person> func2 = Person::new;
        System.out.println(func2.apply("master yi"));
    }
}

 

2.数组引用

public class ArrayRefTest {
    public static void main(String[] args) {
        //lambda
        Function<Integer, String[]> func = length -> new String[length];
        String[] arr1 = func.apply(4);
        System.out.println(Arrays.toString(arr1));

        //数组引用
        Function<Integer, String[]> func2 = String[]::new;
        String[] arr2 = func.apply(5);
        System.out.println(Arrays.toString(arr2));
    }
}

 

posted @ 2021-06-15 22:09  圣金巫灵  阅读(45)  评论(0编辑  收藏  举报