函数式接口也称为 SAM 接口(Single Abstract Method Interface)
其特点是:
- 有且仅有一个抽象方法
- 允许定义静态方法
- 允许定义默认方法
- 可以使用 @FunctionalInterface 注解修饰, 方便编译器检查, 但不是必须的
1. 主要接口
1 2 3 4
| void accept(T t)
default Consumer<T> andThen(Consumer<? super T> after)
|
1 2 3 4 5 6 7 8
| R apply(T t);
default <V> Function<V, R> compose(Function<? super V, ? extends T> before)
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after)
static <T> Function<T, T> identity()
|
1 2 3 4 5 6 7 8
| boolean test(T t);
default Predicate<T> and(Predicate<? super T> other)
default Predicate<T> negate()
default Predicate<T> or(Predicate<? super T> other)
|
2. Resource