当前位置:网站首页>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
边栏推荐
- Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
- [paper reading] cavemix: a simple data augmentation method for brain vision segmentation
- js如何实现数组转树
- Huawei employs data management experts with an annual salary of 2million! The 100 billion market behind it deserves attention
- Summary of week 22-07-02
- Advanced template
- Parameter passing mechanism of member methods
- Skills in analyzing the trend chart of London Silver
- What is the difference between port mapping and port forwarding
- URLs and URIs
猜你喜欢
Application of fire fighting system based on 3D GIS platform
How to do the project of computer remote company in foreign Internet?
巩固表达式C# 案例简单变量运算
Learning of basic amplification circuit
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
Learn C language from scratch day 024
雅思考试流程、需要具体注意些什么、怎么复习?
P3304 [SDOI2013]直径(树的直径)
Design of emergency lighting evacuation indication system for urban rail transit station
OpenHarmony资源管理详解
随机推荐
挖财学院开户安全的吗?开户怎么开?
1189. Maximum number of "balloons"
华为200万年薪聘请数据治理专家!背后的千亿市场值得关注
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
Consolidated expression C case simple variable operation
How many triangles are there in the golden K-line diagram?
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
Parameter passing mechanism of member methods
Illustrated network: what is gateway load balancing protocol GLBP?
基本放大电路的学习
Leetcode70 (Advanced), 322
C语言中sizeof操作符的坑
PyTorch: In-place Operation
Tester's algorithm interview question - find mode
ORB(Oriented FAST and Rotated BRIEF)
Oracle case: SMON rollback exception causes instance crash
npm install报错 强制安装
P3304 [SDOI2013]直径(树的直径)
URL和URI
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)