当前位置:网站首页>Lambda expression
Lambda expression
2022-06-27 00:41:00 【Cold Snowflakes】
Experience lambda expression
// Anonymous inner class mode
new Thread (new Runnable(){
@Override
public void run() {
System.out.println(" The multithreaded program started ");
}
}).start();
// lambda expression
new Thread(
() -> {
System.out.println(" The multithreaded program started "); }
).start();
--------------------------------------------------------------
() -> {
System.out.println(" The multithreaded program started "); }
// The function here is actually to replace this part :
new Runnable(){
@Override
public void run() {
System.out.println(" The multithreaded program started ");
}
}
In fact, the function is to replace the anonymous inner class , It's just to implement an interface , Does not inherit a class .
Lambda The premise of expression :
1. There's an interface .
2. There is and only one abstract method in the interface .
Omit
The type of parameter can be omitted .
If there's only one parameter , Parentheses can be omitted .
If the code block has only one statement , You can omit braces and semicolons of statements . If this statement is return sentence , When omitted , To put return Also omit .
useFlyable(
s -> {
System.out.println(s); }
);
// Equivalent
useFlyable(
s -> System.out.println(s)
);
matters needing attention
1. Use Lambda There has to be an interface , And there is only one abstract method in the interface .
2.lambda The expression actually acts as An object of the implementation class of an interface Role , Parameter is Abstract method Parameters of , The code block is Abstract method Method body of .
3. There must be a context , Can be derived Lambda Corresponding interface
public static void main(String[] args) {
// According to the assignment of local variables Lambda Corresponding interface
Runnable r = () -> System.out.println("Lambda expression ");
// According to the parameters of the calling method Lambda Corresponding interface
new Thread(() -> System.out.println("Lambda expression ")).start();
// The method parameter must be an interface , Cannot be an abstract class perhaps concrete class .
}
And anonymous inner classes
Anonymous inner class , It can be ( Implement an interface , Inherit an abstract class / concrete class ) Object of class .
Lambda expression , Must be ( Implement an interface ) Object of class .
There is more than one method in the interface , Only use Anonymous inner class .
边栏推荐
- In the Internet industry, there are many certificates with high gold content. How many do you have?
- 这3个并发编程的核心,竟然还有人不知道?
- Using physical information neural network to solve hydrodynamics equations
- Hit the point! The largest model training collection!
- “message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
- 巧记大小端字节序
- 不会写免杀也能轻松过defender上线CS
- “message“:“Bad capabilities. Specify either app or appTopLevelWindow to create a session“
- How to use Pinia (I) introduce Pinia into the project
- Encapsulate servlet unified processing request
猜你喜欢

Introduction to message queuing

No clue about complex data?

根据文件名批量生成文件夹

如何通俗易懂的描述机器学习的流程?

不会写免杀也能轻松过defender上线CS

05 | standard design (Part 2): how to standardize the different styles of commit information, which are difficult to read?

墨者学院-SQL注入漏洞测试(报错盲注)
![[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories](/img/ee/b7cb528b79036896da781b73620758.jpg)
[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories

温故知新--常温常新

国内外最好的12款项目管理系统优劣势分析
随机推荐
这3个并发编程的核心,竟然还有人不知道?
国产框架MindSpore联合山水自然保护中心,寻找、保护「中华水塔」中的宝藏生命
小白看MySQL--windows环境安装MySQL
find_ Detailed use guide of CIRC
MATLAB data type - character type
Common techniques of email attachment phishing
[vscade] preview MD file
剑指 Offer 10- II. 青蛙跳台阶问题
滑环选型选购时需要注意的技巧
An article takes you to learn container escape
Alibaba cloud server purchase, basic configuration, (xshell) remote connection and environment building
1+1<2 ?! Interpretation of hesic papers
股票怎样在手机上开户安全吗 网上开户炒股安全吗
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
[vscode] setting sync, a plug-in for synchronizing extensions and settings
test
Memorizing byte order of big and small end
Encapsulate servlet unified processing request
根据文件名批量生成文件夹
巧记大小端字节序