Lambda
- Lambda 是一个匿名函数,可以把Lambda 表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)
- 匿名:没有名称
- 函数:它是函数,Lambda 有参数列表、函数主体、返回类型,还有可能抛出的异常列表
- 传递:Lambda 表达式可以作为参数传递给方法或存储在变量中
- 简洁:无需像匿名类那样写很多模板代码
- 之前:
Comparator<Apple> byWeight = new Comparator<Apple>() {
public int compare(Apple a1, Apple a2){
return a1.getWeight().compareTo(a2.getWeight());
}
}
- 之后(用了Lambda表达式):
Comparator<Apple> byWeight = (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
Lambda 表达式语法
-
Lambda 表达式在Java 语言中引入了一个新的语法元素和操作符。这个操作符为 “ -> ” ,该操作符被称为Lambda 操作符或剪头操作符。它将Lambda 分为两个部分:
- 左侧:指定了Lambda 表达式需要的所有参数
- 右侧:指定了Lambda 体,即Lambda 表达式要执行的功能
-
语法格式介绍
-
语法格式一: 无参,无返回值,Lambda 体只需一条语句
Runnable r1 = () -> System.out.println(“Hello Lambda!”); -
语法格式二: Lambda 需要一个参数
(x) -> System.out.println(x) -
语法格式三: Lambda 只需要一个参数时,参数的小括号可以省略
x -> System.out.println(x) -
语法格式四: 有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
Comparator com = (x, y) -> {
System.out.println(“函数式接口”);
return Integer.compare(x, y);
}; -
语法格式五: 若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
Comparator com = (x, y) -> Integer.compare(x, y); -
语法格式六: Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
(Integer x, Integer y) -> Integer.compare(x, y);
-
-
Lambda 表达式需要“函数式接口”的支持
- 函数式接口: 接口中只有一个抽象方法的接口,称为函数式接口。 可以使用注解 @FunctionalInterface 修饰 可以检查是否是函数式接口
-
函数式接口
- 函数式接口
- 只包含一个抽象方法的接口
- 可以通过Lambda 表达式来创建该接口的对象
- 我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以检查它是否是一个函数式接口
- Java8 内置的四大核心函数式接口
- 函数式接口
函数式接口 | 参数类型 | 返回类型 | 用途 |
---|---|---|---|
Consumer< T >消费型接口 | T | void | 对类型为T的对象应用操作,包含方法 : void accept(T t) |
Supplier< T >供给型接口 | 无 | T | 返回类型为T的对象,包含方法 : T get() |
Function<T, R>函数型接口 | T | R | 对类型为T的对象应用操作,并返回结果。结果是R类型的对象,包含方法 : R apply(T t) |
Predicat< T >断定型接口 | T | boolean | 确定类型为T的对象是否满足某约束,并返回boolean 值。包含方法 : boolean test(T t) |
-
方法引用与构造器引用
-
方法引用
- 当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用
- 实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致
- 方法引用:使用操作符“::” 将方法名和对象或类的名字分隔开来
- 如下三种主要使用情况:
- 对象 :: 实例方法
- 类 :: 静态方法
- 类 :: 实例方法
- 案例
- (x) -> System.out.println(x); 等同于:
System.out::println; - BiPredicate<String, String> bp = (x, y) -> x.equals(y); 等同于:
BiPredicate<String, String> bp2 = String::equals;
- (x) -> System.out.println(x); 等同于:
-
构造器引用
- 格式:ClassName::new
- 与函数式接口相结合,自动与函数式接口中方法兼容
- 可以把构造器引用赋值给定义的方法,与构造器参数列表要与接口中抽象方法的参数列表一致
- 案例
Supplier sup = () -> new Employee();
System.out.println(sup.get());
//构造器引用
Supplier sup2 = Employee::new;
System.out.println(sup2.get());
-
数组引用
- 格式:type[] :: new
- 案例
Function<Integer, String[]> fun = (args) -> new String[args];
//*数组引用
Function<Integer, Employee[]> fun2 = Employee[] :: new;
-
-
Lambda使用案例
// ******** 案列 1 简单实例 ********
//jdk 1.8之前
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Hello World!" + num);
}
};
r.run();
//jdk 1.8 使用Lambda,以下Lambda参数和主体和Runnable接口的抽象方法run()一致,即实现run()方法
Runnable r1 = () -> System.out.println("Hello Lambda!");
r1.run();
// ******** 案列 2 简单实例 ********
//定义数据源
List<Apple> inventory = Arrays.asList(new Apple(80,"green"),new Apple(155, "green"), new Apple(120, "red"));
//过滤方法
public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p){
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
//过滤条件(Lambda表达式)
// [Apple{color='green', weight=155}]
List<Apple> heavyApples2 = filterApples(inventory, (Apple a) -> a.getWeight() > 150);
System.out.println(heavyApples2);
// ******** 案列 3 Java8内置的四大核心函数式接口使用 ********
//Consumer<T> : 消费型接口
// public interface Consumer<T> {
// void accept(T t);
// }
Consumer<Integer> consumer = x -> {
int a = x + 2;
System.out.println(a);// 12
};
consumer.accept(10);
//Supplier<T> : 供给型接口
// public interface Supplier<T> {
// T get();
// }
Supplier<String> supplier = String::new;
System.out.println(supplier.get());//""
Supplier<Emp> supplierEmp = Emp::new;
Emp emp = supplierEmp.get();
emp.setName("Supplier");
System.out.println(emp.getName());//Supplier
//Function<T, R> : 函数型接口
// public interface Function<T, R> {
// R apply(T t);
// }
Function<Integer, Integer> function1 = x -> x * 2;
System.out.println(function1.apply(4));// 8
Function<Integer, String> function2 = x -> x * 2 + "Function";
System.out.println(function2.apply(4));//8Function
Function<String, Emp> objFunction1 = (str) -> new Emp(str);
System.out.println(objFunction1.apply("Function").getName());//Function
//Predicate<T> : 断言型接口
// public interface Predicate<T> {
// boolean test(T t);
// }
// 数字类型 判断值是否大于5
Predicate<Integer> predicate = x -> x > 5;
System.out.println(predicate.test(10));//true
// 字符串的非空判断
Predicate<String> predicateStr = x -> null == x || "".equals(x);
System.out.println(predicateStr.test(""));//true
// ******** 案列 4 方法的引用以及构造器的引用 ********
// 类::静态方法名
Comparator<Integer> cam1 = (x, y) -> x.compareTo(y);
System.out.println(cam1.compare(3, 2));
Comparator<Integer> cam = Integer::compareTo;
System.out.println(cam.compare(3, 2));
// 对象::实例方法名
Consumer<String> con1 = x -> System.out.println(x);
con1.accept("abc");
Consumer<String> con = System.out::println;
con.accept("abc");
Emp emp = new Emp("上海", "xiaoMIng", 18);
Supplier<String> supper1 = () -> emp.getAddress();
System.out.println(supper1.get());
Supplier<String> supper = emp::getAddress;
System.out.println(supper.get());
/*************** 构造器的引用 ****************/
// 无参构造函数,创建实例
Supplier<Emp> supper2 = () -> new Emp();
Supplier<Emp> supper3 = Emp::new;
Emp emp1 = supper3.get();
emp1.setAddress("上海");
// 一个参数
Function<String, Emp> fun = address -> new Emp(address);
Function<String, Emp> fun1 = Emp::new;
System.out.println(fun1.apply("beijing"));