文章目录
- 1 函数式接口
- 1.1 概述
- 1.2 常见函数式接口
- 1.3 常用的默认方法
- 2 方法引用
- 2.1 引用类的静态方法
- 2.2 引用对象的实例方法
- 2.3 引用类的实例方法
- 2.4 构造器引用
1 函数式接口
1.1 概述
只有一个抽象方法的接口我们称之为函数接口。
JDK的函数式接口都加上了@FunctionalInterface
注解进行标识。但是无论是否加上该注解只要接口中只有一个抽象方法,都是函数式接口。
1.2 常见函数式接口
Consumer
消费接口:
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数进行消费。
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
...
}
Function
计算转换接口
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数计算或转换,把结果返回
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
...
}
Predicate
判断接口
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数条件判断,返回判断结果
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
...
}
Supplier
生产型接口
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中创建对象,把创建好的对象返回
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
1.3 常用的默认方法
and
:我们在使用Predicate接口时候可能需要进行判断条件的拼接。而and方法相当于是使用&&
来拼接两个判断条件。
例如:打印作家中年龄大于17并且姓名的长度大于1的作家。
public static void main(String[] args) {
List<Author> authors = getAuthors();
authors.stream()
.filter(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getAge() > 17;
}
}.and(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getName().length() > 1;
}
}))
.forEach(author -> System.out.println(author.getName()));
}
使用lambda表达式简化:
public static void main(String[] args) {
List<Author> authors = getAuthors();
authors.stream()
.filter(((Predicate<Author>) author -> author.getAge() > 17).and(author -> author.getName().length() > 1))
.forEach(author -> System.out.println(author.getName()));
}
or
:我们在使用Predicate接口时候可能需要进行判断条件的拼接。而or方法相当于是使用||
来拼接两个判断条件。
例如:打印作家中年龄大于17或者姓名的长度小于2的作家。
List<Author> authors = getAuthors();
authors.stream()
.filter(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getAge()>17;
}
}.or(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getName().length()<2;
}
})).forEach(author -> System.out.println(author.getName()));
negate
:Predicate接口中的方法。negate方法相当于是在判断添加前面加了个!
表示取反
例如:打印作家中年龄不大于17的作家。
List<Author> authors = getAuthors();
authors.stream()
.filter(new Predicate<Author>() {
@Override
public boolean test(Author author) {
return author.getAge()>17;
}
}.negate()).forEach(author -> System.out.println(author.getAge()));
2 方法引用
我们在使用lambda时,如果方法体中只有一个方法的调用的话(包括构造方法),我们可以用方法引用进一步简化代码。
基本格式:类名或者对象名::方法名
2.1 引用类的静态方法
类名::方法名
如果我们在重写方法的时候,方法体中只有一行代码,并且这行代码是调用了某个类的静态方法,并且我们把要重写的抽象方法中所有的参数都按照顺序传入了这个静态方法中,这个时候我们就可以引用类的静态方法。
2.2 引用对象的实例方法
对象名::方法名
如果我们在重写方法的时候,方法体中只有一行代码,并且这行代码是调用了某个对象的成员方法,并且我们把要重写的抽象方法中所有的参数都按照顺序传入了这个成员方法中,这个时候我们就可以引用对象的实例方法。
2.3 引用类的实例方法
类名::方法名
如果我们在重写方法的时候,方法体中只有一行代码,并且这行代码是调用了第一个参数的成员方法,并且我们把要重写的抽象方法中剩余的所有的参数都按照顺序传入了这个成员方法中,这个时候我们就可以引用类的实例方法。
2.4 构造器引用
如果方法体中的一行代码是构造器的话就可以使用构造器引用。
类名::new
如果我们在重写方法的时候,方法体中只有一行代码,并且这行代码是调用了某个类的构造方法,并且我们把要重写的抽象方法中的所有的参数都按照顺序传入了这个构造方法中,这个时候我们就可以引用构造器。