Function
Function表示接收一个参数T
并返回结果R
的函数。
java
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
* 接受一个参数,处理后并返回结果
* @param t the function argument
* @return the function result
*/
R apply(T t);
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
* 组合两个函数,给定函数返回值为当前函数的参数,并返回结果
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
* 组合两个函数,当前函数的返回值作为给定函数的参数,并返回结果
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* Returns a function that always returns its input argument.
* 返回值恒定为给定参数的函数
* @param <T> the type of the input and output objects to the function
* @return a function that always returns its input argument
*/
static <T> Function<T, T> identity() {
return t -> t;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
JDK中的使用场景
- java.util.Comparator
- java.util.concurrent.CompletableFuture
- java.util.stream.Collectors
- java.util.Map
- java.util.stream.Stream
- java.util.Optional
扩展接口
- 基础接口
- UnaryOperator:出入参都是
T
的Function - BinaryOperator:出入参都是
double
的BiFunction - BiFunction:
T
、U
转R
的Function
- UnaryOperator:出入参都是
R
为int
- ToIntFunction:
T
转int
的Function - IntUnaryOperator:出入参都是
int
的Function - IntBinaryOperator:出入参都是
int
的BiFunction - IntFunction:
int
转R
的Function - ToIntBiFunction:
T
、U
转int
的BiFunction - IntToDoubleFunction:
int
转double
的Function - IntToLongFunction:
int
转long
的Function
- ToIntFunction:
R
为long
- ToLongFunction:
T
转long
的Function - LongUnaryOperator:出入参都是
long
的Function - LongBinaryOperator:出入参都是
long
的BiFunction - LongFunction:
long
转R
的Function - ToLongBiFunction:
T
、U
转long
的BiFunction - LongToIntFunction:
long
转int
的Function - LongToDoubleFunction:
long
转double
的Function
- ToLongFunction:
R
为double
- ToDoubleFunction:
T
转double
的Function - DoubleUnaryOperator:出入参都是
double
的Function - DoubleBinaryOperator:出入参都是
double
的BiFunction - DoubleFunction:
double
转R
的Function - ToDoubleBiFunction:
T
、U
转double
的BiFunction - DoubleToIntFunction:
double
转int
的Function - DoubleToLongFunction:
double
转long
的Function
- ToDoubleFunction: