天天看点

方法引用和构造器引用1. 方法引用(method reference)2. 构造器引用3. 数组引用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hanniel/article/details/81877750

方法引用是Java8 的新特性。

我们知道,使用Lambda表达式可以极大地简化我们的代码,但有时候Lambda体中的功能已经有现成的方法实现了,这时我们可以直接使用方法引用,而不是重复地去实现该功能。

方法引用可以理解为Lambda表达式的另一种表现形式。

方法引用引用的方法的参数列表和返回值类型,必须和函数式接口中抽象方法的参数列表和返回值类型保持一致。

方法引用常用的有3种格式:

public static void main(String[] args) {
        String str = "Hello world";
        Supplier<String> sup = () -> str.toUpperCase();
        System.out.println(sup.get());
    }           

上面的代码中使用了Lambda表达式,表达式中的功能和str对象的toUpperCase()方法一样,因此,可以直接使用方法引用,如下:

public static void main(String[] args) {
        String str = "Hello world";
        //Supplier<String> sup = () -> str.toUpperCase();
        Supplier<String> sup = str::toUpperCase;
        System.out.println(sup.get());
    }           

另一个例子:

public static void main(String[] args) {
        String str = "Hello world";
        //传统的Lambda表达式
        Consumer con = (x) -> System.out.println(x);
        con.accept(str);
        System.out.println("-----------------------");
        // 使用方法引用
        Consumer con1 = System.out::println;
        con1.accept(str);
    }           

public static void main(String[] args) {
        float a = 3.6f; 
        // 传统的Lambda表达式
        Function<Float, Integer> fun = (x) -> Math.round(x);
        System.out.println(fun.apply(a));
        System.out.println("-----------------");
        // 使用方法引用
        Function<Float, Integer> fun1 = Math::round;
        System.out.println(fun.apply(a));
    }           

public class Person {
    public void say() {
        System.out.println("hello");
    }
    public static void main(String[] args) {
        //传统Lambda表达式
        Consumer<Person> con = (x) -> x.say();
        con.accept(new Person());
        System.out.println("-------------------");
        // 使用方法引用
        Consumer<Person> con1 = Person::say;
        con1.accept(new Person());
    }
}           

脚下留心:若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName,如下所示:

// 测试两个字符串是否相等 
    public static void main(String[] args) {
        // 传统的Lambda表达式
        BiPredicate<String, String> bp = (x, y) -> x.equals(y);
        System.out.println(bp.test("abc", "ab"));
        System.out.println("-----------------");
        // 使用方法引用
        BiPredicate<String, String> bp1 = String::equals;
        System.out.println(bp1.test("abc", "ab"));
    }           

使用当构造器的参数列表与函数式接口中抽象方法的参数列表一致。

public static void main(String[] args) {
        // 传统的Lambda表达式
        Function<String, StringBuffer> fun = (x) -> new StringBuffer(x);
        // 使用方法引用
        Function<String, StringBuffer> fun1 = StringBuffer::new;
    }           

数组引用的使用基本和构造器引用相同。

// 生成指定长度的字符串数组
public static void main(String[] args) {
    // 传统的Lambda表达式
    Function<Integer, String[]> fun = (e) -> new String[e];
    String[] str = fun.apply(5);
    System.out.println(str.length);
    System.out.println("---------------------------");
    // 使用方法引用
    Function<Integer, String[]> fun1 = String[]::new;
    String[] str1 = fun1.apply(7);
    System.out.println(str1.length);
}