当前位置:网站首页>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
边栏推荐
- Skills in analyzing the trend chart of London Silver
- TS quick start - functions
- 人脸识别5- insight-face-paddle-代码实战笔记
- 如何避免电弧产生?—— AAFD故障电弧探测器为您解决
- 如何有效对直流列头柜进行监测
- The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
- Application of fire fighting system based on 3D GIS platform
- abc 258 G - Triangle(bitset)
- JS 将伪数组转换成数组
- Leetcode70 (Advanced), 322
猜你喜欢

华为200万年薪聘请数据治理专家!背后的千亿市场值得关注

2022.07.03(LC_6111_统计放置房子的方式数)

uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)

Using fast parsing intranet penetration to realize zero cost self built website

JS how to realize array to tree

青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区

图解网络:什么是网关负载均衡协议GLBP?
![P3304 [SDOI2013]直径(树的直径)](/img/5c/984675bf4517481f80f54657c6c7ad.png)
P3304 [SDOI2013]直径(树的直径)

abc 258 G - Triangle(bitset)

如何避免电弧产生?—— AAFD故障电弧探测器为您解决
随机推荐
[error reporting] "typeerror: cannot read properties of undefined (reading 'split')“
Operator explanation
Advanced template
2022.07.03 (LC 6108 decryption message)
Binary conversion problem
JS convert pseudo array to array
基于三维gis平台的消防系统运用
Distributed base theory
P3304 [SDOI2013]直径(树的直径)
企业应用业务场景,功能添加和修改C#源码
他做国外LEAD,用了一年时间,把所有房贷都还清了
Using fast parsing intranet penetration to realize zero cost self built website
"Xiaodeng" domain password policy enhancer in operation and maintenance
What is the difference between port mapping and port forwarding
How to use fast parsing to make IOT cloud platform
URL和URI
Summer challenge brings you to play harmoniyos multi terminal piano performance
OpenHarmony资源管理详解
Skills in analyzing the trend chart of London Silver
Five papers recommended for the new development of convolutional neural network in deep learning