s-day07--Lambda表达式的简写形式

This commit is contained in:
xx
2026-10-20 10:47:08 +08:00
parent 40756bd111
commit 532d2b2b4e
2 changed files with 8 additions and 2 deletions
+6 -1
View File
@@ -37,7 +37,11 @@ Lambda表达式的所需语法的符号:();->;{}
Lambda表达式的前提条件: Lambda表达式的前提条件:
1.需要一个有且仅有一个抽象方法的接口(函数式接口) 1.需要一个有且仅有一个抽象方法的接口(函数式接口)
2.接口作为参数或者变量类型 2.接口作为参数或者变量类型
-------------------------------------------
在Lambda标准格式的基础上,使用省略写法的规则为:
小括号内参数的类型可以省略;
如果小括号内有且仅有一个参数,则小括号可以省略;
如果大括号内有且仅有一个语句,则无论是否有返回值,都可以同时省略大括号、return关键字及语句分号。
*/ */
public class Demo01 { public class Demo01 {
public static void main(String[] args) { public static void main(String[] args) {
@@ -51,5 +55,6 @@ public class Demo01 {
//-----------------------以下使用函数式编程思想lambda表达式来优化线程代码---------------------------------- //-----------------------以下使用函数式编程思想lambda表达式来优化线程代码----------------------------------
new Thread(()->{System.out.println(Thread.currentThread().getName()+"线程启动");}).start(); new Thread(()->{System.out.println(Thread.currentThread().getName()+"线程启动");}).start();
new Thread(()->System.out.println(Thread.currentThread().getName()+"线程启动")).start();
} }
} }
+2 -1
View File
@@ -25,7 +25,8 @@ public class Demo02 {
} }
});*/ });*/
Collections.sort(students,(Student o1, Student o2)->{return o1.score-o2.score;} ); // Collections.sort(students,(Student o1, Student o2)->{return o1.score-o2.score;} );
Collections.sort(students,( o1, o2)-> o2.score-o1.score );
System.out.println("排序之后:"+students); System.out.println("排序之后:"+students);
} }