Repository navigation
Expand file tree
/
Copy pathFunctionDemo.java
More file actions
75 lines (62 loc) · 1.9 KB
/
Copy pathFunctionDemo.java
File metadata and controls
75 lines (62 loc) · 1.9 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package basic.lambda;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class FunctionDemo {
public void predicateTest() {
Predicate<String> predicate = (s) -> s.length() > 0;
boolean test = predicate.test("test");
System.out.println("字符串长度大于0:" + test);
test = predicate.test("");
System.out.println("字符串长度大于0:" + test);
test = predicate.negate().test("");
System.out.println("字符串长度小于0:" + test);
Predicate<Object> pre = Objects::nonNull;
Object ob = null;
test = pre.test(ob);
System.out.println("对象不为空:" + test);
ob = new Object();
test = pre.test(ob);
System.out.println("对象不为空:" + test);
}
public void functionTest() {
Function<String, Integer> toInteger = Integer::valueOf;
// toInteger的执行结果作为第二个backToString的输入
Function<String, String> backToString = toInteger
.andThen(String::valueOf);
String result = backToString.apply("1234");
System.out.println(result);
Function<Integer, Integer> add = (i) -> {
System.out.println("frist input:" + i);
return i * 2;
};
Function<Integer, Integer> zero = add.andThen((i) -> {
System.out.println("second input:" + i);
return i * 0;
});
Integer res = zero.apply(8);
System.out.println(res);
}
public void supplierTest() {
Supplier<String> supplier = () -> "special type value";
String s = supplier.get();
System.out.println(s);
}
public void consumerTest() {
Consumer<Integer> add5 = (p) -> {
System.out.println("old value:" + p);
p = p + 5;
System.out.println("new value:" + p);
};
add5.accept(10);
}
public static void main(String[] args) {
FunctionDemo fd = new FunctionDemo();
// fd.predicateTest();
// fd.functionTest();
// fd.supplierTest();
fd.consumerTest();
}
}