当前位置:网站首页>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
边栏推荐
- URL和URI
- 22-07-02周总结
- Is it safe to open an account in the College of Finance and economics? How to open an account?
- How to effectively monitor the DC column head cabinet
- Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
- Complete knapsack problem (template)
- Distributed base theory
- [monitoring] ZABBIX
- The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
- [IELTS reading] Wang Xiwei reading P4 (matching1)
猜你喜欢

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

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

lambda expressions

Detailed explanation of openharmony resource management

公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!

ORB(Oriented FAST and Rotated BRIEF)

How to avoid arc generation—— Aafd fault arc detector solves the problem for you

abc 258 G - Triangle(bitset)

It's too convenient. You can complete the code release and approval by nailing it!

【C】(笔试题)指针与数组,指针
随机推荐
【selenium自动化】常用注解
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
兩個數相互替換
Consolidated expression C case simple variable operation
Distributed base theory
【路径规划】RRT增加动力模型进行轨迹规划
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
lambda expressions
Illustrated network: what is gateway load balancing protocol GLBP?
基于三维gis平台的消防系统运用
Identifiers and keywords
What is the difference between port mapping and port forwarding
【C】 (written examination questions) pointer and array, pointer
Introduction to ACM combination counting
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
PyTorch: In-place Operation
Application of multi loop instrument in base station "switching to direct"
Is it safe to open and register new bonds? Is there any risk? Is it reliable?
Build your own minecraft server with fast parsing
How to use fast parsing to make IOT cloud platform