推荐用法
小于 1 分钟函数式编程方法引用
方法体中只有一个方法的调用的话,可以使用方法引用进一步简化代码。是一个语法糖。
推荐先写出lambda表达式,然后使用IDEA快捷键使用方法引用,没有必要死记硬背方法引用规则。
public static void main(String[] args) throws Throwable {
List<Author> authors = getAuthors();
authors.stream()
.map(author -> author.getAge())
.distinct()
.forEach(author -> System.out.println(author));
}
使用IDEA提示转成方法引用:
public static void main(String[] args) throws Throwable {
List<Author> authors = getAuthors();
authors.stream()
.map(Author::getAge)
.distinct()
.forEach(System.out::println);
}