当前位置:网站首页>(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
边栏推荐
- 编程思想比任何都重要,不是比谁多会用几个函数而是比程序的理解
- uniapp 实现微信小程序全局分享及自定义分享按钮样式
- STM32 serial communication principle
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- Electronic product design
- [successful graduation] [1] - visit [student management information system]
- Raspberry pie installation SciPy
- STM32 interrupt switch
- 万字手撕七大排序(代码+动图演示)
- Fundamentals of Electronic Technology (III)_ Chapter 2 principle of amplification circuit__ Crystal triode and field effect triode
猜你喜欢

Project cost management__ Cost management technology__ Article 6 prediction

对于新入行的同学,如果你完全没有接触单片机,建议51单片机入门

单片机职业发展:能做下去的都成牛人了,熬不动就辞职或者改行了

UCI and data multiplexing are transmitted on Pusch (Part V) -- polar coding

MYSQL数据库底层基础专栏
![[CSDN]C1训练题解析_第二部分_Web基础](/img/91/72cdea3eb3f61315595330d2c9016d.png)
[CSDN]C1训练题解析_第二部分_Web基础

How does the nr-prach receiver detect the relationship between prembleid and Ta

SCM career development: those who can continue to do it have become great people. If they can't endure it, they will resign or change their careers
![Successful graduation [2] - student health management system function development...](/img/91/72cdea3eb3f61315595330d2c9016d.png)
Successful graduation [2] - student health management system function development...

Difference of EOF
随机推荐
JMX、MBean、MXBean、MBeanServer 入门
Stm32f04 clock configuration
Epoll read / write mode in LT and et modes
There is no specific definition of embedded system
2020-08-23
[male nanny style] teach you to open the first wechat applet
PIP references domestic sources
[graduation successful] [1] - tour [Student Management Information System]
SSB Introduction (PbCH and DMRs need to be supplemented)
STM32 serial communication principle
UCI and data multiplexing are transmitted on Pusch - placement of data and UCI positions (Part III)
自动装箱与拆箱了解吗?原理是什么?
Runtime.getRuntime().gc() 和 Runtime.getRuntime().runFinalization() 的区别
Project cost management__ Topic of comprehensive calculation
要選擇那種語言為單片機編寫程序呢
Raspberry pie installation SciPy
Project cost management__ Cost management technology__ Article 6 prediction
我想各位朋友都应该知道学习的基本规律就是:从易到难
Programming ideas are more important than anything, not more than who can use several functions, but more than the understanding of the program
Learn the contents of 5g toolbox supporting NR through the NR resources provided by MATLAB