当前位置:网站首页>Lambda表达式
Lambda表达式
2022-07-03 11:46:00 【初夏0811】
一、引入lambda表达式
我们新建一个线程,然后实现runable接口中的run方法:
public class demo1 {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("新线程执行了。。。");
}
}).start();
}
}
如果用lambda表达式可简写为:
public class demo1 {
public static void main(String[] args) {
new Thread(() -> System.out.println("lambda表达式执行了。。。")).start();
}
}
labmda表达式的好处:
1.可以简化匿名内部类,简化代码
二、lambda表达式的标准格式
(参数列表):参数列表
{}:方法体
-> :无特殊意义,连接参数和方法体
1.无参无返回值
public interface Swimingable {
void swiming();
}
public class Swiming {
public static void main(String[] args) {
goSwiming(() ->{
System.out.println("我是lambda游泳");
});
}
public static void goSwiming(Swimingable swimingable){
swimingable.swiming();
}
}
运行如下:
我是lambda游泳
看到方法的参数是接口就可以考虑使用lambda表达式,但不是所有的匿名内部类都可以用;ambda表达式,lambda表达式相当于对接口的重写。
1.有参有返回值
public interface SmokeAble {
int smoke(String name);
}
public class Smoking {
public static void main(String[] args) {
goSmoking((String name) -> {
System.out.println("labmda今天抽了" + name + "的烟");
return 5;
});
}
public static void goSmoking(SmokeAble s) {
int i = s.smoke("黄鹤楼");
System.out.println("返回值:" + i);
}
}
运行结果如下:
labmda今天抽了黄鹤楼的烟
返回值:5
下面来个用lambda实现排序:
public class PersonLambda {
public static void main(String[] args) {
List<Persion> persions = new ArrayList<>();
persions.add(new Persion("刘德华", 55, 167));
persions.add(new Persion("张学友", 58, 174));
persions.add(new Persion("初夏", 42, 180));
Collections.sort(persions, (Persion o1, Persion o2) -> {
return o1.getAge() - o2.getAge();
});
for (Persion persion:persions){
System.out.println(persion);
}
}
}
运行如下:
Persion{
name='初夏', age=42, height=180}
Persion{
name='刘德华', age=55, height=167}
Persion{
name='张学友', age=58, height=174}
三、lambda表达式的省略格式
使用省略的规则如下:
1.小括号内的参数类型可以省略;
2.如果下括号内仅有一个参数,小括号也可以省略;
3.如果大括号内仅有一个语句,可以同时省略大括号,return语句和分号
省略前:
(int a)->{
return new Person();
}
省略后:
a -> new Person()
四、lambda表达式使用的前提条件
lambda表达式语法非常简介,但lambda表达式也不是乱用的,使用的时候有几个条件需要特别注意下:
1.方法的参数或者局部变量的类型必须为接口类型;
2.接口中有且只有一个抽象方法;
只有一个抽象方法的接口称为函数式接口,才可以用lambda表达式;
JDK8新增了一个@FunctionalInterface注解,可以帮助检验函数式接口,如果有多个抽象方法会报错。
边栏推荐
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- Laravel time zone timezone
- repo Manifest Format
- (database authorization - redis) summary of unauthorized access vulnerabilities in redis
- QT OpenGL texture map
- PHP导出word方法(一phpword)
- 为什么我的mysql容器启动不了呢
- Atomic atomic operation
- Systemverilog-- OOP--对象的拷贝
- Deploying WordPress instance tutorial under coreos
猜你喜欢

Php Export word method (One MHT)

Raven2 of vulnhub

Solution to the second weekly test of ACM intensive training of Hunan Institute of technology in 2022

Vulnhub geminiinc V2

AOSP ~ NTP (Network Time Protocol)

Niuniu's team competition

OpenGL index cache object EBO and lineweight mode

Wrong arrangement (lottery, email)

Laravel time zone timezone

Basic knowledge of OpenGL (sort it out according to your own understanding)
随机推荐
Use of QT OpenGL camera
temp
(构造笔记)GRASP学习心得
Develop plug-ins for idea
QT OpenGL rotate, pan, zoom
Solution à la défaillance de l'installation d'Electron
OpenGL 索引缓存对象EBO和线宽模式
During FTP login, the error "530 login incorrect.login failed" is reported
C language improvement article (wchar_t) character type
Summary of development issues
使用BLoC 构建 Flutter的页面实例
AOSP ~ NTP (Network Time Protocol)
257. All paths of binary tree
Laravel time zone timezone
239. Sliding window maximum
PHP get the file list and folder list under the folder
网络通讯之Socket-Tcp(一)
typeScript
Duplicate numbers in the array of sword finger offer 03
Talk about the state management mechanism in Flink framework