当前位置:网站首页>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;
}
}
边栏推荐
- Tables converting to latex format
- Starting Oracle under Linux
- Tianrun cloud is about to be listed: VC tycoon Tian Suning significantly reduces his holdings and is expected to cash out HK $260million
- polardbx是pg还是mysql?
- Interpretation of the paper -- scalable collaborative filtering with jointly derived neighborhood interpolation weights
- CVE-2022-22965复现
- 一文搞懂开放源码软件(OSS)质量保证
- 20 good habits of outstanding professionals
- What is bout?
- 成都测试设备开发_单片机C语言之数组介绍
猜你喜欢

BSN发展联盟理事长单志广:DDC可为中国元宇宙产业发展提供底层支撑

Cve-2022-22965 reappearance
Redis+Caffeine两级缓存的实现

Nine good programming habits for 10 years

安装和使用protobuf-c
How MySQL enables multiple auto incrementing columns in a table

Stm32f1 and stm32subeide programming example - Optical intermittent sensor drive

How to implement interface exception scenario testing? Exploration of test methods and implementation of test tools

Tables converting to latex format

“不敢去懷疑代碼,又不得不懷疑代碼”記一次網絡請求超時分析
随机推荐
Implementation of connecting SQL server to Oracle server_ Including query implementation
哈希索引设计发展史
Stephencovey's tips for efficient work for young people
Query rewriting for opengauss kernel analysis
如何给VR全景作品添加遮罩?作用是什么?
Configuring cplex12.4 tutorial in VS2010
Getting started with go web programming: validators
Some common SQL (version 05 and above) database maintenance scripts
Chip silicon and streaming technology
hw在即,你还不会看危险报文?
Query escape in Oracle expdp export
ThoughtWorks.QRCode和ZXing.Net 二维码,网址可以直接跳转
Loot、USDT
uniapp app 端截屏且保存到本地
"N'osez pas douter du Code, vous devez douter du Code" notez une analyse de délai de demande réseau
【无标题】
Ppt data collection methods and analysis skills
谈谈人生风控
华为这份关于专利的会议纪要,都说了什么?(内含华为十大发明彩蛋)
Redis+Caffeine两级缓存的实现