进阶day06-Lambda表达式的简写形式

This commit is contained in:
2026-03-10 15:02:55 +08:00
parent 9bfa87fd2b
commit 36aaf90ebe
3 changed files with 18 additions and 1 deletions

View File

@@ -45,5 +45,9 @@ public class Demo01 {
}).start();
//-------------------------------以下使用函数式编程思想优化以上的代码lambda----------------------------------
new Thread(()->{ System.out.println(Thread.currentThread().getName()+"线程启动了");}).start();
//以下代码中方法体只有一条java语句省略了大括号分号
new Thread(()->System.out.println(Thread.currentThread().getName()+"线程启动了")).start();
}
}

View File

@@ -27,7 +27,8 @@ public class Demo02 {
}
});*/
Collections.sort(students,(Student o1, Student o2)->{ return o2.score - o1.score;});
// Collections.sort(students,(Student o1, Student o2)->{ return o2.score - o1.score;});
Collections.sort(students,(o1, o2)-> o2.score - o1.score);
System.out.println(students);
}

View File

@@ -0,0 +1,12 @@
package com.inmind.lambda01;
/*
lambda表达式的省略语法:
1.参数列表()中的数据类型可以省略
2.如果(),参数列表中只有一个参数,并且数据类型已经省略,那么小括号也可以省略
3.如果{}中只有一条java语句,那么大括号,return,分号可以同时省略
*/
public class Demo03 {
public static void main(String[] args) {
}
}