当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Memorizing byte order of big and small end

简单快速的数网络(网络中的网络套娃)

07 | 工作流设计:如何设计合理的多人开发模式?

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

超硬核!华为智慧屏上的家庭相册竟可以自动精准分类?

Oracle 数据库基本知识概念

【UVM实战 ===> Episode_3 】~ Assertion、Sequence、Property

技术干货|什么是大模型?超大模型?Foundation Model?

Amway! How to provide high-quality issue? That's what Xueba wrote!

05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?
随机推荐
CPU的异常处理
温故知新--常温常新
com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
基于SSMP的宠物医院管理系统
Why does EDR need defense in depth to combat ransomware?
指南针开户安全的吗?
Memorizing byte order of big and small end
Kubeadm create kubernetes cluster
[微服务]Eureka
泰国安全又划算的支付方式
这10款文案神器帮你速码,做自媒体还担心写不出文案吗?
Mindspire, a domestic framework, cooperates with Shanshui nature conservation center to find and protect the treasure life in the "China water tower"
In depth understanding of UDP in the transport layer and the use of UDP in sockets
接口测试框架实战(一) | Requests 与接口请求构造
如何写好测试用例以及go单元测试工具testify简单介绍
Is it safe to buy pension insurance online? Is there a policy?
剑指 Offer 10- II. 青蛙跳台阶问题
手机炒股靠谱吗 网上开户炒股安全吗
[micro service]nacos
记录一次换行符引起的bug