方法引用和Lambda
方法引用可以被看作仅仅调用特定方法的Lambda的一种快捷 写法。
它的基本思想是,如果一个Lambda代表的只是“直接调用这个方法”,那最好还是用名称 来调用它,而不是去描述如何调用它。
事实上,方法引用就是让你根据已有的方法实现来创建 Lambda表达式。
但是,显式地指明方法的名称,你的代码的可读性会更好。
当你需要使用方法引用时,目标引用放在分隔符::前,方法的名称放在后面。
例如, Apple::getWeight就是引用了Apple类中定义的方法getWeight。
注意:不需要括号
因为 你没有实际调用这个方法。
比如:
Lambda:
inventory.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
使用方法引用和java.util.Comparator.comparing:
inventory.sort(comparing(Apple::getWeight));
Lambda及其等效方法引用的例子
lambda | 等效的方法引用 |
---|---|
(Apple a) -> a.getWeight() | Apple::getWeight |
() -> Thread.currentThread().dumpStack(); | Thread.currentThread()::dumpStack |
(str, i) -> str.substring(i) | String::substring |
(String s) -> System.out.println(s) | System.out::println |
方法引用
方法引用主要有三类:
静态方法的方法引用
例如Integer的parseInt方法,写作Integer::parseInt
List<String> strList = Lists.newArrayList("1", "2", "3");
List<Integer> intList1 = strList.stream().map(item -> Integer.parseInt(item)).collect(Collectors.toList());
List<Integer> intList2 = strList.stream().map(Integer::parseInt).collect(Collectors.toList());
如图所示:
向任意类型实例方法的方法引用
例如String的length 方法,写作String::length
List<String> strList = Lists.newArrayList("xing", "guo");
strList.stream().map(item -> item.length()).forEach(System.out::println);
strList.stream().map(String::length).forEach(System.out::println);
如图所示:
现有对象的实例方法的方法引用
假设有一个局部变量expensiveTransaction 用于存放Transaction类型的对象,它支持实例方法getValue,那么你就可以写expensiveTransaction::getValue
prodList.stream().map(item -> item.getName()).forEach(System.out::println);
prodList.stream().map(Product::getName).forEach(System.out::println);
如图所示:
构造函数引用
对于一个现有构造函数,你可以利用它的名称和关键字new来创建它的一个引用: ClassName::new。
它的功能与指向静态方法的引用类似。
无参
如果有一个构造函数没有参数。 它适合Supplier的签名() -> Apple。
Supplier<Apple> c1 = Apple::new;
Apple a1 = c1.get();
//或者
Supplier<Apple> c1 = () -> new Apple();
Apple a1 = c1.get();
一个参数
如果构造函数的签名是Apple(Integer weight),那么它就适合Function接口的签 名,于是你可以这样写:
Function<Integer, Apple> c2 = Apple::new;
Apple a2 = c2.apply(110);
//或者
Function<Integer, Apple> c2 = (weight) -> new Apple(weight);
Apple a2 = c2.apply(110);
在下面的代码中,一个由Integer构成的List中的每个元素都通过我们前面定义的类似的 map方法传递给了Apple的构造函数,得到了一个具有不同重量苹果的List:
List<Integer> weights = Arrays.asList(7, 3, 4, 10);
List<Apple> apples = map(weights, Apple::new);
public static List<Apple> map(List<Integer> list, Function<Integer, Apple> f){
List<Apple> result = new ArrayList<>();
for(Integer e: list){
result.add(f.apply(e));
}
return result;
}
两个参数
如果有一个具有两个参数的构造函数Apple(String color, Integer weight),那么 它就适合BiFunction接口的签名,于是可以这样写:
BiFunction<String, Integer, Apple> c3 = Apple::new;
Apple c3 = c3.apply("green", 110);
BiFunction<String, Integer, Apple> c3 = (color, weight) -> new Apple(color, weight);
Apple c3 = c3.apply("green", 110);
三个参数
如果是三个参数的构造函数,比如Color(int, int, int),这是需要自己创建一个函数式接口。
public interface TriFunction<T, U, V, R>{
R apply(T t, U u, V v);
}
TriFunction<Integer, Integer, Integer, Color> colorFactory = Color::new;
相关链接:
java8中map新增方法详解
java8中Stream的使用
java8中Collection新增方法详解
java8中Collectors的方法使用实例
java8中常用函数式接口
java8中的方法引用和构造函数引用
java8中的Collectors.groupingBy用法
java8中的Optional用法
java8中的日期和时间API