Lambda
Lambda,表达式是jdk1.8 推出的新特性(语法糖),也可称为闭包,可以快速实现函数式接口方法,配合系统内置的函数式接口,代码会更简洁,增强可读性
1. 函数式接口
-
接口内只有一个抽象方法的接口称为函数式接口
-
函数式接口可以使用@FunctionalInterface 标注
-
@FunctionalInterface interface MyFunctionalInterface { public int fun(); }
2. Lambda基本语法
-
基本结构
- ‘->’ 操作符
- java 8 新增的符号,箭头操作符,Lambda 操作符 ,将表达式分为两部分 ParameterList->FunctionBody
- 参数列表
- 箭头符号的左边部分 参数列表可以省略参数类型
- 方法体
- 方法体中 是对抽象方法的实现
- ‘->’ 操作符
-
基本语法
-
无参数无返回值
-
@Test public void lambdaTest1() { Runnable r=()-> System.out.println('a'); r.run(); }
-
-
无参数有返回值
-
函数式接口
-
@FunctionalInterface interface MyFunctionalInterface { public int fun(); }
-
-
测试
-
@Test public void lambdaTest2() { int a=11; fun(a,()->a+1); //使用Lambda 表达式快速实现函数式接口 } void fun(int a,MyFunctionalInterface functionalInterface) { int b= functionalInterface.fun(); System.out.println(b); }
-
-
-
有参数有返回值
-
@Test public void lambdaTest3() { Integer a=10; Integer b=2; Comparator<Integer> comparator=(x,y)->{ System.out.println(a+" "+b); return x.compareTo(y); }; comparator.compare(a,b); }
-
-
有参数无返回值
-
注意
- 参数列表一定和接口抽象方法一致列表一致
- 参数列表在只有一个参数的时候可以省略括号、方法体只有一条语句时可以省略 ‘{}’
- 闭包内(即Lambda 表达式)的外部变量不能改变只能是 final 虽然没有显示定义 但是可以使 拷贝转移的方式更改
3 .四大内置函数式接口
-
消费型 Consumer
-
Consumer<T>consumer void accept(T t);
-
接受参数 处理 不返回
-
-
供给型 Supplier
-
Supplier<T> supplier T get();
-
不接受参数 根据逻辑提供返回值
-
-
函数型接口 Function
-
Function<T, R> R apply(T t);
-
接受 两个 参数 类型为 T 返回;类型 为 R 经过内部逻辑
-
-
断言型 Predicate
-
Predicate<T> predicate boolean test(T t)
-
接受参数 做出判断 返回Boolean 值
-
注意
- 以上提到只是 四种内置的最基本的函数式子接口
- 基于他们衍生非常多的函数式子接口 我们可以根据需要去选择
4.方法引用
当 Lambda 方法体内部所执行的仅仅是对其他实现的引用**/** 方法体中的内容已经有方法去实现了 ,我们可以使用 方法引用 (一种特殊情况下的特定书写)
-
对象::实例方法名
-
@Testpublic void lambdaTest4(){ PrintStream out = System.out; Consumer<Integer> consumer= out::println; consumer.accept(1000);}//括号直接省略
-
实例
-
public class MyPojo { public boolean changeName(String name) { name = "xhr"; System.out.println(name); return true; }}
-
-
测试
-
@Testpublic void lambdaTest5(){ MyPojo pojo=new MyPojo(); Predicate<String>predicate=pojo::changeName; System.out.println(predicate.test("wbd"));}
-
-
-
类::静态方法名
-
@Testpublic void lambdaTest6(){ Supplier<Double> supplier=Math::random; System.out.println(supplier.get());}
-
-
类::实例方法名
-
特殊要求 (x,y)->{} 参数 1 必须为 参数 2 的调用者 而 参数 2 必须为 实例方法的参数
-
@Testpublic void lambdaTest7() { BiFunction<String, String, Boolean> biFunction =String::equals; //Function 一类的衍生函数式接口 System.out.println(biFunction.apply("lxc", "xhr"));}
-
5 .构造器引用
-
ClassName::new
-
无参
-
@Test public void lambdaTest8() { Supplier<MyPojo> supplier=MyPojo::new; MyPojo myPojo=supplier.get(); }
-
-
有参构造
-
构造器
-
public MyPojo(int age, String name) { this.age = age; this.name = name; }
-
-
测试
-
@Test public void lambdaTest9() { BiFunction<Integer,String,MyPojo> biFunction=MyPojo::new; MyPojo myPojo=biFunction.apply(21,"lxc"); System.out.println(myPojo.toString()); }
-
注意
- 注意函数式接口的参数列表 类型 、数量 ,保持一致
-
-
-
构造数组
-
Type::new
-
@Test public void lambdaTest10() { Function<Integer,String[]>function=String[]::new; String[] apply = function.apply(10); System.out.println(apply.length); //构造数组时一定要给出数组长度 }
-
-