函数式接口
就是只定义一个抽象方法(用来表示行为)的接口,用作Lambda表达式的类型。
public interface Comparator{ int compare(T o1, T o2);}public interface Runnable{ void run();}
java.util.function中给到的部分函数式接口
函数式接口 | 函数描述符 | 方法 | default方法 |
---|---|---|---|
Predicate<T> | T->boolean | test(T t) | and,negate,or,isEqual |
Consumer<T> | T->void | accept(T t) | andThen |
Function<T,R> | T->R | apply(T t) | compose,andThen,identity |
Supplier<T> | ()->T | get() | 无 |
BiPredicate<L,R> | (L,R)->boolean | test(L l, R r) | and,negate,or |
BiConsumer<T,U> | (T,U)->void | accept(T t, U u) | andThen |
BiFunction<T,U,R> | (T,U)->R | apply(T t, U u) | andThen |
UnaryOperator<T> | T->T | ||
BinaryOperator<T> | (T,T)->T |
原始类型特化 |
---|
Predicate |
IntPredicate |
LongPredicate |
DoublePredicate |
Consumer |
IntConsumer |
LongConsumer |
DoubleConsumer |
Function<T,R> |
IntFunction<R>, |
IntToDoubleFunction, |
IntToLongFunction, |
LongFunction<R>, |
LongToDoubleFunction, |
LongToIntFunction, |
DoubleFunction<R>, |
ToIntFunction<T>, |
ToDoubleFunction<T>, |
ToLongFunction<T> |
Supplier<T> |
BooleanSupplier |
IntSupplier |
LongSupplier |
DoubleSupplier |
UnaryOperator<T> |
IntUnaryOperator |
LongUnaryOperator |
DoubleUnaryOperator |
BinaryOperator<T> |
IntBinaryOperator |
LongBinaryOperator |
DoubleBinaryOperator |
BiConsumer<T,U> |
ObjIntConsumer<T> |
ObjLongConsumer<T> |
ObjDoubleConsumer<T> |
BiFunction<T,U,R> |
ToIntBiFunction<T,U> |
ToLongBiFunction<T,U> |
ToDoubleBiFunction<T,U> |