函数式接口

Java的函数式接口主要有4类:

1
2
3
4
5
6
7
Function: 接收参数,并返回结果,主要方法 R apply(T t)

Consumer: 接收参数,无返回结果, 主要方法为 void accept(T t)

Supplier: 不接收参数,但返回结构,主要方法为 T get()

Predicate: 接收参数,返回boolean值,主要方法为 boolean test(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
public static void main(String[] args) {
//Function
Function<Integer, String> toString = (a) -> {
System.out.println("toString");
return a.toString();
};
Function<String, Boolean> print = (a) -> {
System.out.println(a);
return true;
};

toString.andThen(print).apply(123);

Function.identity().apply(123);

//Predicate
Predicate<Integer> greater = (a) -> {
return a > 10;
};

Predicate<Integer> smaller = (a) -> {
return a > 10;
};

greater.and(smaller).test(123);

//Consumer
Consumer<Integer> consumer = (a) -> {
System.out.println(a);
};

Consumer<Integer> printDouble = (a) -> {
System.out.println(a + a);
};

consumer.andThen(printDouble).accept(1);

//Supplier
Supplier<Integer> one = () -> {
return 1;
};

Supplier<Integer> two = () -> {
return 2;
};
System.out.println(one.get());
System.out.println(two.get());

}