当前位置:网站首页>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
边栏推荐
- P4281 [AHOI2008]紧急集合 / 聚会(LCA)
- Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
- URLs and URIs
- abc 258 G - Triangle(bitset)
- ORB(Oriented FAST and Rotated BRIEF)
- Complete knapsack problem (template)
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- JS 将伪数组转换成数组
- Paper notes multi UAV collaborative monolithic slam
- 业务实现-日志写到同一个行数据里面
猜你喜欢

What did I pay for it transfer to testing post from confusion to firmness?

How to effectively monitor the DC column head cabinet

How many triangles are there in the golden K-line diagram?

Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet

如何避免电弧产生?—— AAFD故障电弧探测器为您解决

Detailed explanation of openharmony resource management

Fast analysis -- easy to use intranet security software

Distributed base theory
![[paper reading] Tun det: a novel network for meridian ultra sound nodule detection](/img/25/e2366cabf00e55664d16455a6049e0.png)
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection

圖解網絡:什麼是網關負載均衡協議GLBP?
随机推荐
Some basic functions of enterprise projects are developed, and important things are saved to online first a
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
如何有效对直流列头柜进行监测
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
The pit of sizeof operator in C language
The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
Hologres Query管理及超时处理
雅思考试流程、需要具体注意些什么、怎么复习?
Is it safe to open and register new bonds? Is there any risk? Is it reliable?
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
[IELTS reading] Wang Xiwei reading P3 (heading)
P4281 [ahoi2008] emergency assembly / gathering (LCA)
海思3559万能平台搭建:YUV422的踩坑记录
Summary of week 22-07-02
兩個數相互替換
P3304 [SDOI2013]直径(树的直径)
Hologres query management and timeout processing
Paper notes multi UAV collaborative monolithic slam
Continuous modification of business scenario functions
Build your own minecraft server with fast parsing