一、概述
只有一个抽象方法的接口我们称之为函数接口。
JDK的函数式接口都加上了 @FunctionalInterface 注解进行标识。但是无论是否加上该注解只要接口中只有一个抽象方法,都是函数式接口。
二、常见函数式接口
函数式编程中配合lambda表达式不需要了解每个接口,只需要关注参数和返回值来选取合适的接口即可
2.1 Consumer 消费接口
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数进行消费。
该接口的抽象方法参数为T,且接口的泛型也为T,返回值为void,所以在重写时只能在函数内部去消费该参数
2.2 Function 计算转换接接口
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数计算或转换,把结果返回
参数为T,返回值为R,且在声明接口时,指定的泛型就是<参数类型,返回值类型>,所以在重写方法时,要把T类型转换为R类型
2.3 Predicate 判断接口
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中对传入的参数条件判断,返回判断结果
该接口泛型为T,抽象方法参数类型为T,返回值为boolean,所以重写方法时,可在方法内部进行判断
2.4 Supplier 生产/供给型接口
根据其中抽象方法的参数列表和返回值类型知道,我们可以在方法中创建对象,把创建好的对象返回
接口泛型为T,抽象方法参数为空,返回值为T。所以重写方法时,只能在方法内部创建一个T对象并返回
三、常用的默认方法
3.1 and
我们在使用Predicate接口时候可能需要进行判断条件的拼接。而and方法相当于是使用&&来拼接两个判断条件
例如:
打印作家中年龄大于17并且姓名的长度大于1的作家
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()));
3.2 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()));
3.3 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.getName()));