java8学习教程之函数引用的使用方法
作者:daisy 时间:2023-08-28 12:03:19
前言
我们在上一篇文章 中以实例讲解如何定义和使用 lambda 表达式,以及与其它语言相比,lambda 表达式在 Java 中的特殊规范。并且提到,lambda 表达式可以进一步简化为函数引用。
这篇文章将介绍如何使用函数引用,话不多说了,来一起看看详细的介绍吧。
函数引用的类型
函数引用分为以下四种:
静态函数,比如 Integer 类的 parseInt 函数,可以写作
Integer::parseInt
对象级别函数的引用,比如 String 类的 length 函数,可以写作
String::length
具体实例的函数的引用,比如名称为 expensiveTransaction 的一个实例的 getValue,写作
expensiveTransaction::getValue
构造函数的引用
静态函数
比如:
Function<String, Integer> stringToInteger = (String s) -> Integer.parseInt(s);
可以写作:
Function<String, Integer> stringToInteger = Integer::parseInt;
对象级别函数的引用
BiPredicate<List<String>, String> contains =
(list, element) -> list.contains(element);
可以写作:
BiPredicate<List<String>, String> contains = List::contains;
构造函数的引用
下面再举一个构造函数的例子, 首先定义一个函数式接口,唯一的方法 get 返回指定类型的对象。
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Supplier<TantanitReader> constructor = () -> new TantanitReader();
TantanitReader tantanitReader = constructor.get();
上述代码的 lambda 表达式 new 并返回一个新的对象,使得 constructor 变量成为一个构造器的引用。
等价于以下函数引用:
Supplier<TantanitReader> constructor2 = TantanitReader::new;
TantanitReader tantanitReader2 = constructor2.get();
以上的例子都是没有参数的情况,下面仍然以构造函数为例,介绍有参数的情况:
public TantanitReader(String loginName) {
this.loginName = loginName;
}
Function<String,TantanitReader> constructor3 = (loginName) -> new TantanitReader(loginName);
TantanitReader tantanitReader3 = constructor3.apply("jack");
Function<String,TantanitReader> constructor4 = TantanitReader::new;
TantanitReader tantanitReader4 = constructor4.apply("jack");
TantanitReader tantanitReader5 = constructor4.apply("tom");
这时由于函数只有一个参数,可以使用 Java 自带的 Function这个函数式接口,它实际起作用的函数如下:
R apply(T t);
作用为根据一个参数,返回一个结果。我们由此可以使用 constructor4 以及相应的函数引用 constructor5。
小结
使用函数引用不仅可以简化 lambda 表达式,而且在语义上可以更加专注于方法名称,也就是要做的事,抽象级别更加接近人类的认知。所以,应当尽可能使用函数引用。
来源:http://tantanit.com/java-how-to-use-method-reference/
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# DataSet的内容写成XML时如何格式化字段数据
C#删除只读文件或文件夹(解决File.Delete无法删除文件)
Android RecyclerView详解之实现 ListView GridView瀑布流效果
![](https://img.aspxhome.com/file/2023/9/139309_0s.png)
springboot 整合 sa-token简介及入门教程
![](https://img.aspxhome.com/file/2023/7/83457_0s.png)
java编程之AC自动机工作原理与实现代码
![](https://img.aspxhome.com/file/2023/3/108433_0s.png)
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
![](https://img.aspxhome.com/file/2023/6/61136_0s.jpg)
Java实现word/pdf转html并在线预览
![](https://img.aspxhome.com/file/2023/5/65635_0s.png)
布隆过滤器面试如何快速判断元素是否在集合里
![](https://img.aspxhome.com/file/2023/9/66939_0s.png)
利用Matlab复刻羊了个羊小游戏
![](https://img.aspxhome.com/file/2023/8/131948_0s.jpg)
java合并多个文件的实例代码
Java基础之Spring5的核心之一IOC容器
![](https://img.aspxhome.com/file/2023/2/81192_0s.png)
android中实现指针滑动的动态效果方法
![](https://img.aspxhome.com/file/2023/6/139226_0s.jpg)
C#编程简单实现生成PDF文档的方法示例
Android开发之绘制平面上的多边形功能分析
详解C#中使用对象或集合的初始值设定项初始化的操作
Android图片处理工具类BitmapUtils
C#实现插入排序
![](https://img.aspxhome.com/file/2023/7/102317_0s.png)
Spring JPA之save方法示例详解
![](https://img.aspxhome.com/file/2023/8/67598_0s.png)
java 安全 ysoserial CommonsCollections6 分析
![](https://img.aspxhome.com/file/2023/3/63083_0s.png)
Java生成压缩文件的实例代码
![](https://img.aspxhome.com/file/2023/1/62721_0s.jpg)