当前位置:网站首页>Several simplified forms of lambda expression
Several simplified forms of lambda expression
2022-07-05 00:28:00 【ZZYSY~】
For a functional interface ( There is only one method interface ), We can use lambda Expression to realize its , Here's how to use lambda Several simplified forms of expression
- Conventional form ( There are parameter types , There are brackets , There are curly braces. )
public class lamda {
public static void main(String[] args) {
Hello like = (int i)->{
System.out.println("hello" + i);
};
like.hello(5);
}
}
interface Hello {
void hello(int i);
}
- Simplified parameter type
For methods that have no parameters or one parameter :
Can be simplified
public class lamda {
public static void main(String[] args) {
Hello like = (i)->{
System.out.println("hello" + i);
};
like.hello(5);
}
}
interface Hello {
void hello(int i);
}
For methods with multiple parameters :
To simplify , All simplified
public class lamda {
public static void main(String[] args) {
Hello like = (i,j)->{
System.out.println("hello" + i + j);
};
like.hello(5,"world");
}
}
interface Hello {
void hello(int i,String j);
}
- Simplified brackets
For methods that have no parameters or one parameter :
Can be simplified
public class lamda {
public static void main(String[] args) {
Hello like = i->{
System.out.println("hello" + i);
};
like.hello(5,);
}
}
interface Hello {
void hello(int i);
}
For methods with multiple parameters :
Irreducible parentheses
- Simplify curly braces
For methods with only one line of code :
Can be simplified
public class lamda {
public static void main(String[] args) {
Hello like = (i,j)->System.out.println("hello" + i + j);
like.hello(5,"world");
}
}
interface Hello {
void hello(int i,String j);
}
For methods with multiple lines of code :
Do not simplify curly braces
summary :
- The premise is that the interface is functional ( There is only one method interface )
- All the above simplified forms can be used together , That is, the parameter type can be simplified , You can also simplify parentheses
- lambda An expression can only be simplified to one line if it has only one line of code , If there are many lines , Then wrap it in code blocks .
- Multiple parameters can also be swept parameter types , If you want to get rid of everything , It has to be bracketed
边栏推荐
- It's too convenient. You can complete the code release and approval by nailing it!
- Acrel-EMS综合能效平台在校园建设的意义
- 【雅思阅读】王希伟阅读P3(Heading)
- Business implementation - the log is written to the same row of data
- 《论文笔记》Multi-UAV Collaborative Monocular SLAM
- Is it safe to open and register new bonds? Is there any risk? Is it reliable?
- 如何有效对直流列头柜进行监测
- Advanced template
- Upload avatar on uniapp
- PermissionError: [Errno 13] Permission denied: ‘data. csv‘
猜你喜欢
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
业务场景功能的继续修改
[path planning] RRT adds dynamic model for trajectory planning
Build your own minecraft server with fast parsing
Some basic functions of enterprise projects are developed, and important things are saved to online first a
如何有效对直流列头柜进行监测
Summer challenge brings you to play harmoniyos multi terminal piano performance
Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
Tester's algorithm interview question - find mode
Consolidated expression C case simple variable operation
随机推荐
Date time type and format in MySQL
海思3559万能平台搭建:YUV422的踩坑记录
Application of fire fighting system based on 3D GIS platform
1189. Maximum number of "balloons"
[error reporting] "typeerror: cannot read properties of undefined (reading 'split')“
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
分布式BASE理论
基本放大电路的学习
It's too convenient. You can complete the code release and approval by nailing it!
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
Design of emergency lighting evacuation indication system for urban rail transit station
Get to know ROS for the first time
P4281 [AHOI2008]紧急集合 / 聚会(LCA)
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
Hologres query management and timeout processing
Parameter passing mechanism of member methods
企业应用业务场景,功能添加和修改C#源码
业务实现-日志写到同一个行数据里面
他做国外LEAD,用了一年时间,把所有房贷都还清了
两个数相互替换