函数式接口
用于这些接口作为参数的方法,传入的就是这些接口的Lambda表达式,而这些方法内部调用传入的Lambda表达式实现的接口对象方法,类似于回调函数
Supplier(生产形接口)
@FunctionalInterface
public interface Supplier<T> { T get(); }
Consumer(消费形接口)
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
default Consumer<T> andThen(Consumer<? super T> after) {
//连接多个Consumer接口实现类对象
//此方法后调用accept方法才能实现多个Consumer的accept方法依次调用
//c1.andThen(c2).andThen(c3).accept(s); //c1、c2和c3的accept依次执行
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
Predicate(断言形接口)
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
default Predicate<T> and(Predicate<? super T> other) {
//连接多个Predicate接口实现类对象,短路与操作
//此方法调用后用test方法才能实现多个Predicate的test方法的结果短路与
//p1.and(p2).test(b);//p1.test(b)&&p2/test(b)
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate<T> or(Predicate<? super T> other) {
//连接多个Predicate接口实现类对象,短路或操作
//此方法调用后用test方法才能实现多个Predicate的test方法的结果短路或
//p1.or(p2).test(b);//p1.test(b)||p2/test(b)
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
default Predicate<T> negate() {
//对测试结果取反
//test方法之前调用可以使测试结果取反操作
//p.negate().test();
return (t) -> !test(t);
}
...
}
Function(功能形接口)
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
//连接多个Function接口实现类对象
//此方法后调用apply方法才能实现多个Function的apply方法向外嵌套调用
//f1.andThen(f2).apply(s); //f2.apply(f1.apply());
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
...
}

Comments NOTHING