函数式接口函数描述原始类型特化
PredicateT -> booleanIntPredicate,
LongPredicate,
DoublePredicate
ConsumerT -> voidIntConsumer,
LongConsumer,
DoubleConsumer
Function<T, R>T -> RIntFunction,
IntToDoubleFunction,
IntToLongFunction,
LongFunction,
LongToDoubleFunction,
LongToIntFunction,
DoubleFunction,
ToIntFunction,
ToDoubleFunction,
ToLongFunction
Supplier() -> TBooleanSupplier,
IntSupplier,
LongSupplier,
DoubleSupplier
UnaryOperatorT -> TIntUnaryOperator,
LongUnaryOperator,
DoubleUnaryOperator
BinaryOperator(T, T) -> TIntBinaryOperator,
LongBinaryOperator,
DoubleBinaryOperator
BiPredicate<L, R>(L, R) -> boolean
BiConsumer<T, U>(T, U) -> voidObjIntConsumer,
ObjLongConsumer,
ObjDoubleConsumer
BiFunction<T, U, R>(T, U) -> RToIntBiFunction<T, U>,
ToLongBiFunction<T, U>,
ToDoubleBiFunction<T, U>

Lambdas 及函数式接口的例子

使用案例Lambda 的例子对应的函数式接口
布尔表达式(List list) -> list.isEmpty()Predicate<List>
创建对象() -> new Apple(10)Supplier
消费一个对象(Apple a) -> System.out.println(a.getWeight())Consumer
从一个对象中选择/提取(String s) -> s.length()Function<String, Integer>,
ToIntFunction
合并两个值(int a, int b) -> a * bIntBinaryOperator
比较两个对象(Apple a1, Apple a2) ->
a1.getWeight().compareTo(a2.getWeight())
Comparator,
BiFunction<Apple, Apple, Integer>,
ToIntBiFunction<Apple, Apple>

2 Replies to “Java8 常用的函数式接口”

发表评论

电子邮件地址不会被公开。 必填项已用*标注