-
函数式接口 的定义
只包含一个抽象方法的接口称为函数式接口
注: 可以添加注解
@FunctionalInterface
到函数式接口
上来检查其是否为正确的函数式接口;
- java 内置的四大核心函数式接口
- 消费型接口
Consumer<T>
其方法如下:
accept方法
其有一个 默认方法andThen:
默认方法andThen
例子:
Consumer接口例子
输出结果:
Consumer接口例子结果
- 供给型接口
Supplier<T>
其方法如下:
get方法
- 函数型接口
Function<T, R>
其方法如下:
apply方法
静态方法:
identity
两个默认方法:
compose andThen默认方法
- 判定型接口
Predicate<T>
其方法如下:
test方法
静态方法:
isEqual
例子:
isEqual例子
默认方法:
and or negate 默认方法
- 其他内置的函数式接口
类 型 | 主 要 方 法 |
---|---|
BiPredicate<T, U> |
boolean test(T t, U u); |
BooleanSupplier |
boolean getAsBoolean(); |
BiConsumer<T, U> |
void accept(T t, U u); |
ObjIntConsumer<T> |
void accept(T t, int value); |
ObjDoubleConsumer<T> |
void accept(T t, double value); |
ObjLongConsumer<T> |
void accept(T t, long value); |
BiFunction<T, U, R> |
R apply(T t, U u); |
UnaryOperator<T> |
T apply(T t); |
BinaryOperator<T> |
T apply(T t1, T t2); |
ToIntFunction<T> |
int apply(T t); |
ToIntBiFunction<T, U> |
int apply(T t, U u); |
ToDoubleFunction<T> |
double apply(T t); |
ToDoubleBiFunction<T, U> |
double apply(T t, U u); |
ToLongFunction<T> |
long apply(T t); |
ToLongBiFunction<T, U> |
long apply(T t, U u); |
与基本类型 int
相关的函数式接口(8个):
IntConsumer
IntPredicate
IntSupplier
IntFunction<R>
UnaryOperator
BinaryOperator
IntToDoubleFunction
IntToLongFunction
与基本类型 long
相关的函数式接口(8个):
LongConsumer
LongPredicate
LongSupplier
LongFunction<R>
LongUnaryOperator
LongBinaryOperator
LongToIntFunction
LongToDoubleFunction
与基本类型 double
相关的函数式接口(8个)
DoubleConsumer
DoublePredicate
DoubleSupplier
DoubleFunction<R>
DoubleUnaryOperator
DoubleBinaryOperator
DoubleToIntFunction
DoubleToLongFunction
网友评论