定义
- 有且仅有一个抽象方法的接口,排除(默认,静态,私有)方法
@FunctionalInterface
- 标明接口是一个函数式接口,如果不是则在编译阶段报错
JDK 提供函数式接口
- Consumer 消费型接口
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
- Supplier 供给型接口
@FunctionalInterface
public interface Supplier<T> {
T get();
}
- Predicate 断定型接口
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
- Function 函数式接口
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
- remark
对于使用函数时接口为入参的情况,可以使用方法作为实参
1:函数式接口相当于方法的泛型
2:执行函数式接口的方法时,实际调用实参传入的方法
3:实参,和形参的参数个数,类型要保持一致