当前位置:网站首页>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
边栏推荐
- NPM install error forced installation
- Basic points of the game setup of the points mall
- 挖财学院开户安全的吗?开户怎么开?
- 人生无常,大肠包小肠, 这次真的可以回家看媳妇去了。。。
- Learn C language from scratch day 024
- Acrel-EMS综合能效平台在校园建设的意义
- Build your own minecraft server with fast parsing
- P4408 [noi2003] truant children (tree diameter)
- AcWing164. 可达性统计(拓扑排序+bitset)
- [Peking University] tensorflow2.0-1-opening
猜你喜欢
图解网络:什么是网关负载均衡协议GLBP?
Learn C language from scratch day 024
Identifiers and keywords
【雅思阅读】王希伟阅读P4(matching1)
Illustrated network: what is gateway load balancing protocol GLBP?
【selenium自动化】常用注解
The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
A new method for analyzing the trend chart of London Silver
随机推荐
Microservice
Build your own minecraft server with fast parsing
lambda expressions
Verilog tutorial (11) initial block in Verilog
海思3559万能平台搭建:YUV422的踩坑记录
What did I pay for it transfer to testing post from confusion to firmness?
【雅思阅读】王希伟阅读P4(matching1)
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
Skills in analyzing the trend chart of London Silver
Detailed explanation of openharmony resource management
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
How to save your code works quickly to better protect your labor achievements
Recursive execution mechanism
ORB(Oriented FAST and Rotated BRIEF)
Hisilicon 3559 universal platform construction: YUV422 pit stepping record
URLs and URIs
Enterprise application business scenarios, function addition and modification of C source code
2022.07.03(LC_6109_知道秘密的人数)
雅思考试流程、需要具体注意些什么、怎么复习?