当前位置:网站首页>11 方法引用和构造器应用
11 方法引用和构造器应用
2022-06-22 13:04:00 【水无痕simon】
- 若lambda体中的内容有方法已经实现了,那么我们可以使用“方法引用”(可以视为lambda的另外一种形式)
主要有3种引用方式:
(1)对象::实例方法名
(2)类::静态方法名
(3)类::实例方法名
public class LambdaTest {
public static void main(String[] args) {
//1. 实例::实例方法名
Consumer<String> con = (x) -> System.out.println(x);
/** * 2. 实例方法名 * 注意:函数接口的放回值和实例方法的返回值,参数必须保持一致 */
PrintStream ps = System.out;
Consumer<String> consumer = ps::println;
//方式三:
Consumer<String> consumer1 = System.out::println;
consumer1.accept("ss");
/** * Supplier接口是存在返回值的,此处如果是getAge则会报错 * 参数类型必须保持一致 */
Employee employee = new Employee();
Supplier<String> sup = () -> employee.getName();
String s = sup.get();
System.out.println(s);
Supplier<Integer> supplier = () -> employee.getAge();
Integer result = supplier.get();
System.out.println(result);
}
}
类::静态方法
public class LambdaTest {
public static void main(String[] args) {
/** * 1. 类::静态方法 * 注意: lambda体和引用的参数列表和返回值类型保持一致 */
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
//等同于
Comparator<Integer> comparator = Integer::compare;
}
}
边栏推荐
- Seven cattle cloud upload picture
- Understand the quality assurance of open source software (OSS)
- HW is around the corner. Can't you read the danger message?
- 谈谈人生风控
- Stephencovey's tips for efficient work for young people
- 成都测试设备开发_单片机C语言之数组介绍
- 论文专利博客写作总结
- Flink status management
- BSN发展联盟理事长单志广:DDC可为中国元宇宙产业发展提供底层支撑
- Temporary recommendation on graphs via long- and short term preference fusion
猜你喜欢

坚持了 10 年的 9 个编程好习惯

hw在即,你还不会看危险报文?

LDA study notes

Cat agile team coaching workshops - August 20

How to understand fold change? Multiple analysis?

Query rewriting for opengauss kernel analysis

client-go gin的简单整合九-Create

一文搞懂开放源码软件(OSS)质量保证

Oceanbase database helps the ideal automobile intelligent production line to realize automatic recovery within 30 seconds

My suggestions on SAP ABAP transformation
随机推荐
金融应用如何解决金额精度问题(以Quorum、golang为例)
Cve - 2022 - 22965 Resume
Neuron+ekuiper realizes data collection, cleaning and anti control of industrial Internet of things
想知道股票开户优惠链接,如何得知?网上开户安全么?
3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
Z-Score和deltf/f有什么区别?
History of hash index design
Should programmers choose software talent outsourcing companies?
Word skills summary
Starting Oracle under Linux
Getting started with go web programming: validators
Transformers vit image model vector acquisition
如何保护WordPress网站免受网络攻击?采取安全措施至关重要
Seven cattle cloud upload picture
微服务测试效率治理
polardbx是pg还是mysql?
Simple integration of client go gin IX create
测试组的任务职责和测试的基本概念
Oceanbase database helps the ideal automobile intelligent production line to realize automatic recovery within 30 seconds
client-go gin的简单整合九-Create