当前位置:网站首页>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 .
边栏推荐
- 【UVM实战 ===> Episode_3 】~ Assertion、Sequence、Property
- 串口调试工具 mobaxterm 下载
- 全網最全的混合精度訓練原理
- Competition Registration | one of the key ai+ scientific computing competitions - China open source scientific software creativity competition, competing for 100000 bonus!
- PHP code audit series (I) basis: methods, ideas and processes
- 【Mysql】时间字段默认设置为当前时间
- find_circ详细使用指南
- Nacos安装指南
- matlab数据类型 —— 字符型
- 技术干货|什么是大模型?超大模型?Foundation Model?
猜你喜欢
![The [MySQL] time field is set to the current time by default](/img/40/5f1d3448259ab703c4b5dc29713a99.png)
The [MySQL] time field is set to the current time by default

Super hard core! Can the family photo album on Huawei's smart screen be classified automatically and accurately?

这3个并发编程的核心,竟然还有人不知道?

The most complete hybrid precision training principle in the whole network
![[微服务]Nacos](/img/69/6641e943c4366d5591acdf9e12389c.png)
[微服务]Nacos

Simulation of delta variant strain of novel coronavirus (mindsponge application)

Can't write to avoid killing and can easily go online CS through defender

05 | 規範設計(下):commit 信息風格迥异、難以閱讀,如何規範?

redis详细教程

1+1<2 ?! HESIC论文解读
随机推荐
全網最全的混合精度訓練原理
Is it safe to buy pension insurance online? Is there a policy?
温故知新--常温常新
大赛报名 | AI+科学计算重点赛事之一——中国开源科学软件创意大赛,角逐十万奖金!
Your connection is not private
复杂数据没头绪?
Can I open an account for stock trading on my mobile phone? Is it safe to open an account for stock trading on the Internet
目前哪个证券公司炒股开户是最好最安全的?
[micro service]nacos
手机上可以开户炒股吗 网上开户炒股安全吗
[微服务]Eureka
安利!如何提优质的ISSUE?学霸是这样写的!
Cve-2022-30190 follina office rce analysis [attached with customized word template POC]
串口调试工具 mobaxterm 下载
这3个并发编程的核心,竟然还有人不知道?
Nacos installation guide
[微服务]认识微服务
com. fasterxml. jackson. databind. exc.MismatchedInputException: Expected array or string. at [Source:x
Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
能在手机上开户炒股吗 网上开户炒股安全吗