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

Java 8 的 43 个函数总结

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

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

1、5类函数

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

类别 数量 汇总 描述 方法
Function 17 Function
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();
Consumer 8 Consumer
BiConsumer
类型Consumer *3
Obj类型Consumer *3
消费者。
1~2参数+0返回
void accept(T t);
void accept(T t, U u);
default andThen(after);
Operator 8 UnaryOperator
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);
Predicate 5 Predicate
BiPredicate
DoublePredicate
IntPredicate
LongPredicate
断言,谓语。
1~2参数+1返回
boolean test(T t);
default and(other);
default negate();
default or(other);
static isEqual(targetRef);
Supplier 5 Supplier
BooleanSupplier
DoubleSupplier
IntSupplier
LongSupplier
提供者。
0参数+1返回
T get();

2、4个参数类别

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

类别 数量 汇总 描述 方法
4 5个基本类别中除了Operator
Consumer、Function、Predicate、Supplier
一个参数的 xxx(T t);
Bi 6 BiConsumer
BiFunction
BiPredicate
ToDoubleBiFunction
ToIntBiFunction
ToLongBiFunction
两个参数的 xxx(T t, U u);
Unary 4 UnaryOperator
DoubleUnaryOperator
IntUnaryOperator
LongUnaryOperator
一元操作 R apply(T t);
static identity();
Binary 4 BinaryOperator
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、类型

Long 9
Int 9
Double 9
Obj 3 ObjDoubleConsumer ObjIntConsumer ObjLongConsumer
Boolean 1 BooleanSupplier
ToLong 4
ToInt 4
ToDouble 4

4、附录

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

编号 原始类名称 限定符1 限定符2 末尾
1 BiConsumer Bi Consumer
2 BiFunction Bi Function
3 BinaryOperator Binary Operator
4 BiPredicate Bi Predicate
5 BooleanSupplier Boolean Supplier
6 Consumer Consumer
7 DoubleBinaryOperator Double Binary Operator
8 DoubleConsumer Double Consumer
9 DoubleFunction Double Function
10 DoublePredicate Double Predicate
11 DoubleSupplier Double Supplier
12 DoubleToIntFunction Double ToInt Function
13 DoubleToLongFunction Double ToLong Function
14 DoubleUnaryOperator Double Unary Operator
15 Function Function
16 IntBinaryOperator Int Binary Operator
17 IntConsumer Int Consumer
18 IntFunction Int Function
19 IntPredicate Int Predicate
20 IntSupplier Int Supplier
21 IntToDoubleFunction Int ToDouble Function
22 IntToLongFunction Int ToLong Function
23 IntUnaryOperator Int Unary Operator
24 LongBinaryOperator Long Binary Operator
25 LongConsumer Long Consumer
26 LongFunction Long Function
27 LongPredicate Long Predicate
28 LongSupplier Long Supplier
29 LongToDoubleFunction Long ToDouble Function
30 LongToIntFunction Long ToInt Function
31 LongUnaryOperator Long Unary Operator
32 ObjDoubleConsumer Obj Double Consumer
33 ObjIntConsumer Obj Int Consumer
34 ObjLongConsumer Obj Long Consumer
35 Predicate Predicate
36 Supplier Supplier
37 ToDoubleBiFunction ToDouble Bi Function
38 ToDoubleFunction ToDouble Function
39 ToIntBiFunction ToInt Bi Function
40 ToIntFunction ToInt Function
41 ToLongBiFunction ToLong Bi Function
42 ToLongFunction ToLong Function
43 UnaryOperator Unary Operator

4.1 将拆分后的关键字分类

类型1 类型2 参数类型 函数类型
ToDouble Double Bi Consumer
ToInt Int Binary Function
ToLong Long Unary Operator
Obj Predicate
Boolean Supplier

猜你喜欢

转载自blog.csdn.net/booynal/article/details/129950762