当前位置:网站首页>(1) 什么是Lambda表达式
(1) 什么是Lambda表达式
2022-07-03 09:15:00 【look-word】
1 什么是Lambda表达式
Runnable接口
@FunctionalInterface // 被该注解修饰的接口只能声明一个抽象方法
public interface Runnable {
public abstract void run();
}
代码结构
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("新线程中执行的代码 :"+Thread.currentThread().getName());
}
}).start();// 不是用lambda的创建线程的写法
以上述代码为例:
- 都知道 不能直接调用接口 要摸就是类实现接口 然后实例化调用方法
- 或者直接采用 匿名类部类的方式 Lambda就是简化匿名类部类的写法
- 传统的方式 一般都采用 上述代码 或则类似的
使用Lambda简化
当然还可以做到更加简化 在下面会详细介绍
new Thread(() ->{
System.out.println("新线程中执行的代码 :"+Thread.currentThread().getName());
}).start();
2 Lambda的语法规则
Lambda省去了面向对象的条条框框,Lambda的标准格式由3个部分组成:
(参数类型 参数名称) -> {
代码体;
}
格式说明:
- (参数类型 参数名称):参数列表
- {代码体;} :方法体
- ->: 箭头,分割参数列表和方法体
简单练习:
- 无参
public class Demo1Lambda {
public static void main(String[] args) {
// 开启一个新的线程
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("新线程中执行的代码 :"+Thread.currentThread().getName());
}
}).start();// 不是用lambda的创建线程的写法
new Thread(() ->{
// 调用无参的方法
System.out.println("新线程中执行的代码 :"+Thread.currentThread().getName());
}).start();
System.out.println("主线程中的代码:" + Thread.currentThread().getName());
}
}
3 @FunctionalInterface注解
/** * @FunctionalInterface * 这是一个标志注解,被该注解修饰的接口只能声明一个抽象方法 * 多个会编译不通过 */
@FunctionalInterface
public interface UserService {
void show();
}
4 Lambda表达式的省略写法
在lambda表达式的标准写法基础上,可以使用省略写法的规则为:
- 小括号内的参数类型可以省略
- 如果小括号内有且仅有一个参数,则小括号可以省略
- 如果大括号内有且仅有一个语句,可以同时省略大括号,return 关键字及语句分号。
接口
public interface StudentService {
String show(String name, Long id) ;
}
代码实现
public static void main(String[] args) {
goStudent((String name,Long id) ->{
return name+id;
});
// lambda简化后的代码
goStudent((String name,Long id) -> name+id+666);
}
// 自定义的方法
public static void goStudent(StudentService studentService){
System.out.println(studentService.show("张三", 22L));
}
- 一般我们都会根据代码的可读性 适度的去简化
5 Lambda表达式的使用前提
Lambda表达式的语法是非常简洁的,但是Lambda表达式不是随便使用的,使用时有几个条件要特别注
意
- 方法的参数或局部变量类型必须为接口才能使用Lambda
- 接口中有且仅有一个抽象方法(@FunctionalInterface)
6 Lambda和匿名内部类的对比
Lambda和匿名内部类的对比
- 所使用场景的类型不一样
- 匿名内部类的类型可以是 类,抽象类,接口
- Lambda表达式需要的类型必须是接口
- 抽象方法的数量不一样
- 匿名内部类所需的接口中的抽象方法的数量是随意的
- Lambda表达式所需的接口中只能有一个抽象方法
- 实现原理不一样
- 匿名内部类是在编译后形成一个class
- Lambda表达式是在程序运行的时候动态生成class
边栏推荐
- 学习开发没有捷径,也几乎不存在带路会学的快一些的情况
- CEF download, compile project
- Fundamentals of Electronic Technology (III)__ Logic gate symbols in Chapter 5
- MySQL data manipulation language DML common commands
- STM32 serial port usart1 routine
- Nr--- Pusch I: sorting out the agreement process
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
- UCI and data multiplexing are transmitted on Pusch (Part VI) -- LDPC coding
- Embedded systems are inherently flawed. Compared with the Internet, there are so many holes that it is simply difficult to walk away from
猜你喜欢
The third paper of information system project manager in soft examination
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
[male nanny style] teach you to open the first wechat applet
Electronic product design, MCU development, circuit cloning
There is no specific definition of embedded system
Nodemcu-esp8266 development board to build Arduino ide development environment
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
An executable binary file contains more than machine instructions
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)
STM32 external interrupt experiment
随机推荐
STM32 serial communication principle
UCI and data multiplexing are transmitted on Pusch - Part I
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Schematic diagram and connection method of six pin self-locking switch
CEF下载,编译工程
Nr--- Pusch I: sorting out the agreement process
【順利畢業】[1]-遊覽 [學生管理信息系統]
Leetcode daily question (2109. adding spaces to a string)
Introduction to chromium embedded framework (CEF)
STM32 port multiplexing and remapping
Flink learning notes (IX) status programming
【顺利毕业】[1]-游览 [学生管理信息系统]
应用最广泛的8位单片机当然也是初学者们最容易上手学习的单片机
Electronic product design, MCU development, circuit cloning
How MySQL modifies null to not null
Leetcode daily question (931. minimum falling path sum)
Successful graduation [3]- blog system update...
The cyclic shift of PUCCH in NR channel is generated by MATLAB
Leetcode daily question (2090. K radius subarray averages)
Mysql database underlying foundation column