java复习第7天---7.2----Lambda表达式
目录
内容
2、Lambda表达式
2.1、产生背景
-
函数式编程思想:有输入和输出的一套算法,即"拿什么东西多什么"。强调做什么,而不是怎么做
-
面向对象编程思想:强调通过对象形式做什么
-
冗余的Runnable代码
package thread.pool; public class TestThread1 { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { System.out.println("多线程任务"); } }).start(); } }
-
分析:
- 匿名内部类的好处和弊端:一方面匿名内部类,省去实现类的定义;另一方面,匿名内部类语法–很复杂
- 语法分析
- Runnable接口只有一个run方法
- 无参数
- 无返回值
- 代码块(方法体):逻辑任务
-
结论:我们只想关心任务逻辑部分,不想关心其他部分。如现实生活中我们想去北京,我们不关心做什么交通工具去,只要到北京就可以。
-
Lambda语法
- () -> System.out.println(“多线程任务”);
- () : 对应run中的参数
- -> :表示前面参数传递给后面的代码块
- 后面语句: 业务逻辑
2.2、标准格式
(参数列表) -> { 任务代码块 }
-
参数列表: 参数
-
-> : 表示前面参数传递给后面
-
{} : 表示业务逻辑部分
- 当{}内只有一条语句的时候,{}可以省略
- 当只有一条return 语句的时候,return 可以省略
-
示例2.2-1:lambda多线程实现
package thread.lambda; public class TestLambda1 { public static void main(String[] args) { new Thread(() -> System.out.println("多线程任务")).start(); } } 测试结果: 多线程任务
2.3、无参无返回值
- 示例2.3-1:同上
2.4、有参有返回值
package thread.lambda;
public interface Calculator {
int add(int a, int b); // 返回 a + b 的值
}
package thread.lambda;
public class TestCalculator {
public static void main(String[] args) {
invokeCalc(3, 4, (int a, int b) -> a + b);
}
public static void invokeCalc(int a, int b, Calculator calculator) {
int result = calculator.add(a, b);
System.out.println(a + "+" + b + "=" + result);
}
}
测试结果:
3+4=7
2.5、省略规则
- 小括号内参数类型可以省略
- 如果小括号内有且仅有一个参数,小括号可以省略
- 如果大括号内有且仅有一个参数,无论是否有返回值,都可以省略大括号、return关键字及语句分号
2.5、使用限制
-
使用Lambda语法必须有接口,且要求接口中有且仅有1个抽象方法:无论java内置Runnable,Comparator或者自定义的接口,只有当接口中抽象方法存在且唯一时,才可以使用Lambda接口
-
使用Lambda语句必须有上下文推断:方法的参数或者局部变量类型必须为Lambda对应的类型,才能使用Lambda作为该接口的实例
3、案例
-
自定义学生类型,根据年龄升序排序
package array.sort; public class Student { private String name; private int age; private int score; public Student(String name, int age, int score) { super(); this.name = name; this.age = age; this.score = score; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", score=" + score + "]"; } } package array.sort; import java.util.Arrays; import java.util.Comparator; public class TestStudent1 { public static void main(String[] args) { Student s1 = new Student("赵刚", 18, 51); Student s2 = new Student("王雪", 20, 95); Student s3 = new Student("张小强", 21, 60); Student s4 = new Student("小丽", 22, 77); Student[] arr = new Student[4]; arr[0] = s4; arr[1] = s2; arr[2] = s3; arr[3] = s1; System.out.println(Arrays.toString(arr)); Arrays.sort(arr, (o1, o2) -> o1.getAge() - o2.getAge()); System.out.println(Arrays.toString(arr)); } } 测试结果: [Student [name=小丽, age=22, score=77], Student [name=王雪, age=20, score=95], Student [name=张小强, age=21, score=60], Student [name=赵刚, age=18, score=51]] [Student [name=赵刚, age=18, score=51], Student [name=王雪, age=20, score=95], Student [name=张小强, age=21, score=60], Student [name=小丽, age=22, score=77]]
后记 :
本项目为参考某马视频开发,相关视频及配套资料可自行度娘或者联系本人。上面为自己编写的开发文档,持续更新。欢迎交流,本人QQ:806797785
前端项目源代码地址:https://gitee.com/gaogzhen/vue-leyou
后端JAVA源代码地址:https://gitee.com/gaogzhen/JAVA