JDK 8 新特性-Lambda表达式方法引用与构造器引用

目录

1、方法引用(method reference)

1.1 实例对象::实例方法名

1.2 类名::静态方法名

1.3 类名::实例方法名

2、构造器引用

2.1 类名::new

3. 数组引用

3.1 类型[]::new


当Lambda体的操作已经有实现的方法,可以使用方法引用!方法引用可以理解为Lambda表达式的另一种表现形式。

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

1、方法引用(method reference)

方法引用:使用操作符 “::” 将方法名和对象或类的名字分隔开来。方法引用常用的有3种格式:

  • 对象::实例方法
  • 类::静态方法
  • 类::实例方法

 

1.1 实例对象::实例方法名

下面的代码中使用了Lambda表达式方法引用,表达式中的功能和System.out对象的println()方法参数列表和返回值一样,,可以直接使用方法引用

 public class ReferenceExample01 {
    public static void main(String[] args) {
        // Lambda表达式
        Consumer<String> consumer1 = (x) -> System.out.println(x);
        consumer1.accept("lambda");
        // 方法引用
        Consumer<String> consumer2 = System.out::println;
        consumer2.accept("reference");
    }
}

 

public class ReferenceExample02 {
    public static void main(String[] args) {
        // Lambda 表达式
        String str = "referenceExample02";
        Supplier<String> supplier1 = () -> str.toUpperCase();
        System.out.println(supplier1.get());
        // 方法引用
        Supplier<String> supplier2 = str::toUpperCase;
        System.out.println(supplier2.get());
    }
}

 

1.2 类名::静态方法名

 public class ReferenceExample03 {
    public static void main(String[] args) {
        // Lambda 表达式
        Comparator<Integer> comparator1 = (x,y) -> Integer.compare(x,y);
        System.out.println(comparator1.compare(10,20));
        // 方法引用
        Comparator<Integer> comparator2 = Integer::compare;
        System.out.println(comparator2.compare(10,20));
    }
}

 

public class ReferenceExample04 {
    public static void main(String[] args) {
        float num = 1.9f;
        // Lambda表达式
        Function<Float, Integer> function1 = (x) -> Math.round(num);
        System.out.println(function1.apply(num));
        // 方法引用
        Function<Float, Integer> function2 = Math::round;
        System.out.println(function2.apply(num));
    }
}

 

1.3 类名::实例方法名

若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,可以使用类名:实例方法名调用,语法格式: ClassName::method。

 

public class ReferenceExample05 {
    public static void main(String[] args) {
        // Lambda表达式
        BiPredicate<String, String> biPredicate1 = (x, y) -> x.equals(y);
        System.out.println(biPredicate1.test("lambda", "Lambda"));
        // 方法引用
        BiPredicate<String, String> biPredicate2 = String::equals;
        System.out.println(biPredicate2.test("lambda", "Lambda"));
    }
}

 

public class ReferenceExample06 {
    public void getInfo() {
        System.out.println("ReferenceExample06");
    }


    public static void main(String[] args) {
        ReferenceExample06 reference = new ReferenceExample06();
        // Lambda表达式
        Consumer<ReferenceExample06> con = (x) -> x.getInfo();
        con.accept(reference);
        // 方法引用
        Consumer<ReferenceExample06> con1 = ReferenceExample06::getInfo;
        con1.accept(reference);
    }
}

 

2、构造器引用

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

2.1 类名::new

 public class ReferenceExample07 {
    public static void main(String[] args) {
        // Lambda表达式
        Supplier<ReferenceExample07> supplier1 = () -> new ReferenceExample07();
        // 构造器引用
        Supplier<ReferenceExample07> supplier2 = ReferenceExample07::new;
    }
}

 

public class ReferenceExample08 {
    public static void main(String[] args) {
        // Lambda表达式
        Function<String, StringBuffer> function1 = (x) -> new StringBuffer(x);
        // 构造器引用
        Function<String, StringBuffer> function2 = StringBuffer::new;
    }
}

 

3. 数组引用

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

3.1 类型[]::new

 public class ReferenceExample09 {
    public static void main(String[] args) {
        // Lambda表达式
        Function<Integer, String[]> function1 = (x) -> new String[x];
        String[] str1 = function1.apply(10);
        System.out.println(str1.length);
        // 数组引用
        Function<Integer, String[]> function2 = String[]::new;
        String[] str2 = function2.apply(10);
        System.out.println(str2.length);
    }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值