Java 8 的 43 个函数总结,函数式编程

Java 8 的 43 个函数总结

根据Java8的源码,分析统计和总结得出该文档。

统计:
总共43个函数类
按函数的类别可分为5类
按参数个数可分为4类
按类型可以分为5类
继承关系:2个。大多数的函数类是没有继承关系的,仅仅靠类名称上的语义来保证关系的递进

1、5类函数

根据类名称,将末尾的关键字提取出来分组,得到5个类别:

类别数量汇总描述方法
Function17Function
BiFunction
类型Function *3
类型To类型Function *6
To类型Function *3
To类型BiFunction *3
函数。
1~2参数+1返回
R apply(T t);
R apply(T t, U u);
default compose(before);
default andThen(after);
static identity();
Consumer8Consumer
BiConsumer
类型Consumer *3
Obj类型Consumer *3
消费者。
1~2参数+0返回
void accept(T t);
void accept(T t, U u);
default andThen(after);
Operator8UnaryOperator
BinaryOperator
类型UnaryOperator *3
类型BinaryOperator *3
操作。
1~2参数+1返回
R apply(T t);
R apply(T t, U u);
int applyAsInt(int operand);
static identity();
static minBy(comparator);
static maxBy(comparator);
Predicate5Predicate
BiPredicate
DoublePredicate
IntPredicate
LongPredicate
断言,谓语。
1~2参数+1返回
boolean test(T t);
default and(other);
default negate();
default or(other);
static isEqual(targetRef);
Supplier5Supplier
BooleanSupplier
DoubleSupplier
IntSupplier
LongSupplier
提供者。
0参数+1返回
T get();

2、4个参数类别

所有的函数被划分为1个参数的和2个参数的。

类别数量汇总描述方法
45个基本类别中除了Operator
Consumer、Function、Predicate、Supplier
一个参数的xxx(T t);
Bi6BiConsumer
BiFunction
BiPredicate
ToDoubleBiFunction
ToIntBiFunction
ToLongBiFunction
两个参数的xxx(T t, U u);
Unary4UnaryOperator
DoubleUnaryOperator
IntUnaryOperator
LongUnaryOperator
一元操作R apply(T t);
static identity();
Binary4BinaryOperator
DoubleBinaryOperator
IntBinaryOperator
LongBinaryOperator
二元操作R apply(T t, U u);
static minBy(comparator);
static maxBy(comparator);

2.1、“N个参数”与“N元操作”的区别:

N元区别继承关系
Unary与函数的区别: 一元操作的参数与返回值类型必须相同;函数的参数与返回类型可以不同UnaryOperator extends Function<T, T>
Binary与2参数函数区别: 二元操作的两个操作数、返回值类型必须是相同;而2参数函数则无此要求BinaryOperator extends BiFunction<T,T,T>

3、类型

Long9
Int9
Double9
Obj3ObjDoubleConsumer ObjIntConsumer ObjLongConsumer
Boolean1BooleanSupplier
ToLong4
ToInt4
ToDouble4

4、附录

原始的43个函数类,以及分解过程

编号原始类名称限定符1限定符2末尾
1BiConsumerBiConsumer
2BiFunctionBiFunction
3BinaryOperatorBinaryOperator
4BiPredicateBiPredicate
5BooleanSupplierBooleanSupplier
6ConsumerConsumer
7DoubleBinaryOperatorDoubleBinaryOperator
8DoubleConsumerDoubleConsumer
9DoubleFunctionDoubleFunction
10DoublePredicateDoublePredicate
11DoubleSupplierDoubleSupplier
12DoubleToIntFunctionDoubleToIntFunction
13DoubleToLongFunctionDoubleToLongFunction
14DoubleUnaryOperatorDoubleUnaryOperator
15FunctionFunction
16IntBinaryOperatorIntBinaryOperator
17IntConsumerIntConsumer
18IntFunctionIntFunction
19IntPredicateIntPredicate
20IntSupplierIntSupplier
21IntToDoubleFunctionIntToDoubleFunction
22IntToLongFunctionIntToLongFunction
23IntUnaryOperatorIntUnaryOperator
24LongBinaryOperatorLongBinaryOperator
25LongConsumerLongConsumer
26LongFunctionLongFunction
27LongPredicateLongPredicate
28LongSupplierLongSupplier
29LongToDoubleFunctionLongToDoubleFunction
30LongToIntFunctionLongToIntFunction
31LongUnaryOperatorLongUnaryOperator
32ObjDoubleConsumerObjDoubleConsumer
33ObjIntConsumerObjIntConsumer
34ObjLongConsumerObjLongConsumer
35PredicatePredicate
36SupplierSupplier
37ToDoubleBiFunctionToDoubleBiFunction
38ToDoubleFunctionToDoubleFunction
39ToIntBiFunctionToIntBiFunction
40ToIntFunctionToIntFunction
41ToLongBiFunctionToLongBiFunction
42ToLongFunctionToLongFunction
43UnaryOperatorUnaryOperator

4.1 将拆分后的关键字分类

类型1类型2参数类型函数类型
ToDoubleDoubleBiConsumer
ToIntIntBinaryFunction
ToLongLongUnaryOperator
ObjPredicate
BooleanSupplier
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值