当前位置:网站首页>(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
边栏推荐
- Flink learning notes (XI) table API and SQL
- 【22毕业季】我是毕业生yo~
- 我想各位朋友都应该知道学习的基本规律就是:从易到难
- MySQL 数据库基础知识(系统化一篇入门)
- Stm32f04 clock configuration
- Difference of EOF
- Seven sorting of ten thousand words by hand (code + dynamic diagram demonstration)
- Project cost management__ Cost management technology__ Article 6 prediction
- 单片机现在可谓是铺天盖地,种类繁多,让开发者们应接不暇
- When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
猜你喜欢

Leetcode daily question (2212. maximum points in an archery competition)

Shell logic case

Intelligent home design and development

Stm32-hal library learning, using cubemx to generate program framework

The cyclic shift of PUCCH in NR channel is generated by MATLAB

Oracle数据库 SQL语句执行计划、语句跟踪与优化实例

Nr-prach:prach format and time-frequency domain

要选择那种语言为单片机编写程序呢

Gpiof6, 7, 8 configuration

UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding
随机推荐
2021-09-26
numpy. Reshape() and resize() functions
MySQL的简单使用(增删改查)
STM32 serial port usart1 routine
When you need to use some functions of STM32, but 51 can't realize them, 32 naturally doesn't need to learn
Process communication - semaphore
Fundamentals of Electronic Technology (III)__ Chapter 1 resistance of parallel circuit
Oracle数据库 SQL语句执行计划、语句跟踪与优化实例
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
内存数据库究竟是如何发挥内存优势的?
307. Range Sum Query - Mutable
Happy Dragon Boat Festival—— Zongzi written by canvas~~~~~
[csdn] C1 analyse des questions de formation Partie III Bar _ JS Foundation
[successful graduation] [1] - visit [student management information system]
MySQL data manipulation language DML common commands
JMX、MBean、MXBean、MBeanServer 入门
QT qcombobox QSS style settings
All processes of top ten management in project management
我想各位朋友都应该知道学习的基本规律就是:从易到难
PRACH --- originator