当前位置:网站首页>(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
边栏推荐
- You need to use MySQL in the opening experiment. How can you forget the basic select statement? Remedy is coming~
- MYSQL数据库底层基础专栏
- Nr--- Pusch I: sorting out the agreement process
- [combinatorics] Introduction to Combinatorics (context of combinatorics | skills of combinatorics | thought of combinatorics 1: one-to-one correspondence)
- uniapp 实现微信小程序全局分享及自定义分享按钮样式
- [CSDN] C1 training problem analysis_ Part IV_ Advanced web
- 【22毕业季】我是毕业生yo~
- Schematic diagram and connection method of six pin self-locking switch
- Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
- 【顺利毕业】[1]-游览 [学生管理信息系统]
猜你喜欢
uniapp 实现微信小程序全局分享及自定义分享按钮样式
Vector processor 9_ Basic multilevel interconnection network
Project cost management__ Topic of comprehensive calculation
Development of fire evacuation system
Comment la base de données mémoire joue - t - elle l'avantage de la mémoire?
JMX、MBean、MXBean、MBeanServer 入门
内存数据库究竟是如何发挥内存优势的?
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 4 --blinker_ DHT_ WiFi (lighting technology app control + temperature and humidity data app display)
SSB Introduction (PbCH and DMRs need to be supplemented)
新系列单片机还延续了STM32产品家族的低电压和节能两大优势
随机推荐
Leetcode daily question (2109. adding spaces to a string)
Hal library sets STM32 clock
Characteristics of PUCCH formats
Schematic diagram and connection method of six pin self-locking switch
编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
[CSDN]C1训练题解析_第二部分_Web基础
顺利毕业[3]-博客系统 更新中。。。
Install local sources using yum
[male nanny style] teach you to open the first wechat applet
Oracle database SQL statement execution plan, statement tracking and optimization instance
[CSDN] C1 training problem analysis_ Part IV_ Advanced web
Fundamentals of Electronic Technology (III)__ Chapter 6 combinational logic circuit
MySQL data manipulation language DML common commands
顺利毕业[2]-学生健康管理系统 功能开发中。。。
Learn the contents of 5g toolbox supporting NR through the NR resources provided by MATLAB
Fundamentals of Electronic Technology (III)_ Integrated operational amplifier and its application__ Basic arithmetic circuit
[graduation successful] [1] - tour [Student Management Information System]
307. Range Sum Query - Mutable
Education is a pass and ticket. With it, you can step into a higher-level environment
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 5 --blinker_ MIOT_ MULTI_ Outside (lighting technology app + Xiaoai classmate control socket multiple jacks)