一、lambda表达式
例如:
Comparator<Integer> comparator=(x,y)->Integer.compare(x,y);
基础语法
- -> 箭头运算符,将参数列表和方法体隔离开
- 左侧: 参数列表
- 右侧: 方法体、lambda体
口诀:
- 写死小括号,拷贝右箭头,落地大括号
- 左右遇一括号省
- 左侧推断类型省
解释:
- 无论左边还是右边,如果参数列表、或者方法体中执行语句只有一个或者一条,可以省略掉小括号和大括号
- 左侧参数列表可以不写参数类型,因为JVM可以根据“类型推断”自动确认
语法格式
主要有以下语法格式
- 无参数无返回值
Runnable r1=new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
};
Runnable r2 = () -> {
System.out.println("hello");
};
如上:因为run方法没有参数,所以左侧括号为空,箭头右侧是run方法的实现体
- 一个参数,无返回值
Consumer<String> con=(str)->{
System.out.println(str);
};
con.accept("哈哈哈,我真是太帅了");
- 一个参数,无返回值 (小括号可以省略不写)
Consumer<String> con=str->System.out.println(str);
con.accept("哈哈哈,我真是太帅了");
- 两个及以上的参数,有返回值,并且 Lambda 体中有多条语句
Comparator<Integer> comparator2 = (x, y) -> {
System.out.println("hello");
return Integer.compare(x, y);
};
- 两个及以上的参数,有返回值,并且 Lambda 体中只有1条语句 (大括号 与 return 都可以省略不写)
Comparator<Integer> comparator1 = (x, y) -> Integer.compare(x, y);
二、函数式接口
接口中只有一个抽象方法的接口 @FunctionalIterface,默认方法不算,必须只有一个抽象接口。个人觉得可能和类型推断等有关,否则无法确认Lamdba表达式实现的式哪一个方法
自定义函数式接口
//自定义一个接口,并标注FunctionalInterface注解,接口内有一个getVlaue抽象方法
@FunctionalInterface
public interface MyFun {
Integer getValue(Integer num);
}
//新建测试类,并调用方法
@Test
public void test() {
Integer num = operateMyFun(10, x -> x * x);
System.out.println(num);
MyFun myFun=x-> x+x;
System.out.println(myFun.getValue(10));
}
private Integer operateMyFun(Integer num, MyFun myFun) {
return myFun.getValue(num);
}
四大内置函数式接口
函数式接口 | 参数类型 | 返回类型 | 用途 |
---|---|---|---|
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) |
消费型接口
Consumer<String> con=(str)->{
System.out.println(str);
};
con.accept("哈哈哈,我真是太帅了");
提供型接口
Supplier<Integer> supplier = () -> (int) (Math.random() * 100);
System.out.println(supplier.get());
函数型接口
Function<String,String> function=str->str.toUpperCase(Locale.ROOT);
System.out.println(function.apply("wo zhen shi tai hao kan le"));
断言型接口
Predicate<String> predicate = str -> {
System.out.println("开始进行判断......");
return "666".equals(str);
};
System.out.println(predicate.test("666"));
其他接口
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kiEb7KnT-1663981833267)(https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/3d238cdf28624779a95b46ec517b7dcf~tplv-k3u1fbpfcp-watermark.image?)]
三、引用
若 Lambda 表达式体中的内容已有方法实现,则我们可以使用“方法引用”
3.1 方法引用
对象::实例方法
Lambda 表达实体中调用方法的参数列表、返回类型必须和函数式接口中抽象方法保持一致
Consumer<String> con3 = s -> System.out.println(s);
Consumer<String> con4 = System.out::println;
con3.accept("我太帅了");
con4.accept("我太帅了");
//解释:调用的Println方法返回类型和Consumer中的accept方法一致为void,并且参数列表也一致
类::静态方法
和上述要求一致
//示例
Comparator<Integer> comparator1 = (x, y) -> Integer.compare(x, y);
Comparator<Integer> comparator2 = (x, y) -> {
System.out.println("hello");
return Integer.compare(x, y);
};
Comparator<Integer> comparator3=Integer::compare;
类::实例方法
Lambda 参数列表中的第一个参数是方法的调用者,第二个参数是方法的参数时,才能使用 ClassName :: Method
//示例
BiPredicate<String,String> b1=(x,y)->x.equals(y);
BiPredicate<String,St