当前位置:网站首页>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 .
边栏推荐
- Concepts de base de données Oracle
- 【Vscode】预览md文件
- 手机能开户炒股吗 网上开户炒股安全吗
- 墨者学院-X-Forwarded-For注入漏洞实战
- [microservices] Understanding microservices
- Lwip之定时机制
- [微服务]Eureka
- Hit the point! The largest model training collection!
- 这10款文案神器帮你速码,做自媒体还担心写不出文案吗?
- Le principe le plus complet de formation à la précision hybride pour l'ensemble du réseau
猜你喜欢

Memorizing byte order of big and small end

Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"

Lorsque le transformateur rencontre l'équation différentielle partielle

全网最全的混合精度训练原理

CPU的异常处理

滑环安装有哪些技巧和方法

温故知新--常温常新

Installation of xshell and xftp

Moher College -x-forwarded-for injection vulnerability practice

Kubernetes visual interface dashboard
随机推荐
PHP code audit series (I) basis: methods, ideas and processes
[微服务]Eureka
全网最全的混合精度训练原理
1+1<2 ?! Interpretation of hesic papers
[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
Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X
Kubeadm create kubernetes cluster
Safe and cost-effective payment in Thailand
Moher College - SQL injection vulnerability test (error reporting and blind note)
不会写免杀也能轻松过defender上线CS
05 | standard design (Part 2): how to standardize the different styles of commit information, which are difficult to read?
The [MySQL] time field is set to the current time by default
新型冠状病毒变异Delta毒株的模拟(MindSPONGE应用)
敲重点!最全大模型训练合集!
matlab数据类型 —— 字符型
05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?
Intrusion trace cleaning
2022 Health Expo, Shandong health care exhibition, postpartum health and sleep health exhibition
高清滑环生产过程当中的质量如何把控
论文学习——降雨场次划分方法对降雨控制率的影响分析