一、前言
- Lambda 表达式
- 函数式接口
- 方法引用 / 构造器引用
- Stream API
- 接口中的默认方法 / 静态方法
- 新时间日期 API
- 其他新特性
新特性
- 速度更快
- 代码更少
- 强大的 Stream API
- 便于并行
- 最大化减少空指针异常 Optional (Kotlin ?)
二、Lambda
2.1 匿名函数
Lambda是一个匿名函数,可以理解为一段可以传递的代码(将代码像数据一样传递);可以写出更简洁、更灵活的代码;作为一种更紧凑的代码风格,是Java语言表达能力得到提升。
@Test
public void test02(){
// Lambda 表达式
Comparator<Integer> comparator1 = (a, b) -> Integer.compare(a, b);
TreeSet<Integer> set = new TreeSet<>(comparator1);
System.out.println("**********************************");
// 方法引用
Comparator<Integer> comparator2 = Integer :: compare(a, b);
TreeSet<Integer> set = new TreeSet<>(comparator2);
}
2.2 Lambda表达式格式
- 操作符:->
- 左侧:参数列表
- 右侧:执行代码块 / Lambda 体
口诀:
- 写死小括号,拷贝右箭头,落地大括号
- 左右遇一括号省
- 左侧推断类型省
2.3 Lambda表达式的使用(6种情况)
语法格式一:无参,无返回值:() -> sout
public class Test {
int num = 10; //jdk 1.7以前 必须final修饰
@Test
public void test01(){
//匿名内部类
new Runnable() {
@Override
public void run() {
//在局部类中引用同级局部变量
//只读
System.out.println("Hello World" + num);
}
};
}
@Test
public void test02(){
//语法糖
Runnable runnable = () -> {
System.out.println("Hello Lambda");
};
}
}
语法格式二:有一个参数,无返回值
// 有一个参数,无返回值
@Test
public void test01(){
Consumer<String> consumer = (String a) -> {
System.out.println(a)
};
consumer.accept("我觉得还行!");
}
语法格式三:有一个参数,无返回值(数据类型可以省略,因为编译器推断得出,称为“类型推断”)
@Test
public void test01(){
Consumer<String> consumer = (a) -> {
System.out.println(a)
};
consumer.accept("我觉得还行!");
}
语法格式四:有一个参数,无返回值(小括号可以省略不写)
// 有一个参数,无返回值 (小括号可以省略不写)
@Test
public void test02(){
Consumer<String> consumer = a -> System.out.println(a);
consumer.accept("我觉得还行!");
}
语法格式五:有两个及以上的参数,有返回值,并且 Lambda 体中有多条语句
@Test
public void test(){
Comparator<Integer> comparator = (a, b) -> {
System.out.println("比较接口");
return Integer.compare(a, b);
};
}
语法格式六:有两个及以上的参数,有返回值,并且 Lambda 体中只有1条语句 (大括号 与 return 都可以省略不写)
@Test
public void test04(){
Comparator<Integer> comparator = (a, b) -> Integer.compare(a, b);
}
Lambda表达式的本质
作为接口的实例
2.4 案例
**案例一:**调用 Collections.sort() 方法,通过定制排序 比较两个 Employee (先按照年龄比,年龄相同按照姓名比),使用 Lambda 表达式作为参数传递
- 定义实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private Integer id;
private String name;
private Integer age;
private Double salary;
}
- 定义 List 传入数据
List<Employee> emps = Arrays.asList(
new Employee(101, "Z3", 19, 9999.99),
new Employee(102, "L4", 20, 7777.77),
new Employee(103, "W5", 35, 6666.66),
new Employee(104, "Tom", 44, 1111.11),
new Employee(105, "Jerry", 60, 4444.44)
);
- @Test
@Test
public void test01(){
Collections.sort(emps, (e1, e2) -> {
if (e1.getAge() == e2.getAge()){
return e1.getName().compareTo(e2.getName());
} else {
return Integer.compare(e1.getAge(), e2.getAge());
}
});
for (Employee emp : emps) {
System.out.println(emp);
}
}
**案例二:**声明函数式接口,接口中声明抽象方法,String getValue(String str); 声明类 TestLambda,类中编写方法使用接口作为参数,将一个字符串转换成大写,并作为方法的返回值;再将一个字符串的第二个和第四个索引位置进行截取字串
**案例三:**声明一个带两个泛型的函数式接口,泛型类型为<T, R> T 为参数,R 为返回值;接口中声明对应的抽象方法;在 TestLambda 类中声明方法,使用接口作为参数,计算两个 Long 类型参数的和;在计算两个 Long 类型参数的乘积
3. 函数式接口
Java内置四大核心函数式接口:
函数式接口 | 参数类型 | 返回类型 | 用途 |
---|---|---|---|
Consumer 消费型接口 |
T | void | 对类型为T的对象应用操作:void accept(T t) |
Supplier 提供型接口 |
无 | T | 返回类型为T的对象:T get() |
Function<T, R> 函数型接口 |
T | R | 对类型为T的对象应用操作,并返回结果为R类型的对象:R apply(T t) |
Predicate 断言型接口 |
T | boolean | 确定类型为T的对象是否满足某约束,并返回boolean值:boolean test(T t) |
3.1 消费型接口
@Test
public void test01(){
//Consumer
Consumer<Integer> consumer = (x) -> System.out.println("消费型接口" + x);
//test
consumer.accept(100);
}
3.2 提供型接口
@Test
public void test02(){
List<Integer> list = new ArrayList<>();
List<Integer> integers = Arrays.asList(1,2,3);
list.addAll(integers);
//Supplier<T>
Supplier<Integer> supplier = () -> (int)(Math.random() * 10);
list.add(supplier.get());
System.out.println(supplier);
for (Integer integer : list) {
System.out.println(integer);
}
}
3.3 函数型接口
@Test
public void test03(){
//Function<T, R>
String oldStr = "abc123456xyz";
Function<String, String> function = (s) -> s.substring(1, s.length()-1);
//test
System.out.println(function.apply(oldStr));
}
3.4 断言型接口
@Test
public void test04(){
//Predicate<T>
Integer age = 35;
Predicate<Integer> predicate = (i)