java8中的方法引用和构造函数引用

本文详细介绍了Java 8中方法引用和构造函数引用的使用,包括静态方法引用、实例方法引用和构造函数引用的不同场景。通过具体示例,展示了如何使用方法引用简化Lambda表达式的编写,提高代码的可读性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法引用和Lambda

方法引用可以被看作仅仅调用特定方法的Lambda的一种快捷 写法。

它的基本思想是,如果一个Lambda代表的只是“直接调用这个方法”,那最好还是用名称 来调用它,而不是去描述如何调用它

事实上,方法引用就是让你根据已有的方法实现来创建 Lambda表达式。

但是,显式地指明方法的名称,你的代码的可读性会更好。

当你需要使用方法引用时,目标引用放在分隔符::前,方法的名称放在后面。

例如, Apple::getWeight就是引用了Apple类中定义的方法getWeight。

注意:不需要括号

因为 你没有实际调用这个方法。


比如:

Lambda:

inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight())); 

使用方法引用和java.util.Comparator.comparing:

inventory.sort(comparing(Apple::getWeight));

Lambda及其等效方法引用的例子
lambda等效的方法引用
(Apple a) -> a.getWeight()Apple::getWeight
() -> Thread.currentThread().dumpStack();Thread.currentThread()::dumpStack
(str, i) -> str.substring(i)String::substring
(String s) -> System.out.println(s)System.out::println

方法引用

方法引用主要有三类:

静态方法的方法引用

例如Integer的parseInt方法,写作Integer::parseInt

List<String> strList = Lists.newArrayList("1", "2", "3");

List<Integer> intList1 = strList.stream().map(item -> Integer.parseInt(item)).collect(Collectors.toList());

List<Integer> intList2 = strList.stream().map(Integer::parseInt).collect(Collectors.toList());

如图所示:
在这里插入图片描述


向任意类型实例方法的方法引用

例如String的length 方法,写作String::length

List<String> strList = Lists.newArrayList("xing", "guo");

strList.stream().map(item -> item.length()).forEach(System.out::println);

strList.stream().map(String::length).forEach(System.out::println);

如图所示:
在这里插入图片描述


现有对象的实例方法的方法引用

假设有一个局部变量expensiveTransaction 用于存放Transaction类型的对象,它支持实例方法getValue,那么你就可以写expensiveTransaction::getValue

prodList.stream().map(item -> item.getName()).forEach(System.out::println);
prodList.stream().map(Product::getName).forEach(System.out::println);

如图所示:
在这里插入图片描述


构造函数引用

对于一个现有构造函数,你可以利用它的名称和关键字new来创建它的一个引用: ClassName::new。
它的功能与指向静态方法的引用类似。

无参

如果有一个构造函数没有参数。 它适合Supplier的签名() -> Apple。

Supplier<Apple> c1 = Apple::new; 
Apple a1 = c1.get(); 

//或者
Supplier<Apple> c1 = () -> new Apple();
Apple a1 = c1.get();
一个参数

如果构造函数的签名是Apple(Integer weight),那么它就适合Function接口的签 名,于是你可以这样写:

Function<Integer, Apple> c2 = Apple::new;
Apple a2 = c2.apply(110);

//或者
Function<Integer, Apple> c2 = (weight) -> new Apple(weight);
Apple a2 = c2.apply(110); 

在下面的代码中,一个由Integer构成的List中的每个元素都通过我们前面定义的类似的 map方法传递给了Apple的构造函数,得到了一个具有不同重量苹果的List:

List<Integer> weights = Arrays.asList(7, 3, 4, 10); 
List<Apple> apples = map(weights, Apple::new); 
public static List<Apple> map(List<Integer> list, Function<Integer, Apple> f){     
    List<Apple> result = new ArrayList<>(); 
    for(Integer e: list){
        result.add(f.apply(e));
    }
    return result; 
} 
两个参数

如果有一个具有两个参数的构造函数Apple(String color, Integer weight),那么 它就适合BiFunction接口的签名,于是可以这样写:

BiFunction<String, Integer, Apple> c3 = Apple::new;
Apple c3 = c3.apply("green", 110); 
BiFunction<String, Integer, Apple> c3 = (color, weight) -> new Apple(color, weight);
Apple c3 = c3.apply("green", 110);  
三个参数

如果是三个参数的构造函数,比如Color(int, int, int),这是需要自己创建一个函数式接口。

public interface TriFunction<T, U, V, R>{
    R apply(T t, U u, V v);
}

TriFunction<Integer, Integer, Integer, Color> colorFactory = Color::new; 

相关链接:
java8中map新增方法详解
java8中Stream的使用
java8中Collection新增方法详解
java8中Collectors的方法使用实例
java8中常用函数式接口
java8中的方法引用和构造函数引用
java8中的Collectors.groupingBy用法
java8中的Optional用法
java8中的日期和时间API

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值