java 基础学习之Java.util.function 简单使用

Java.util.function 简单使用

Java.util.function 简单使用

  • 有很多函数
    • Consumer 接受一个入参,无返回值。
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
@Data
public class TestArgumentFunction {

private String a = "1";
private Integer b = 0;
private Long c = 9L;
private boolean d = true;

@Test
public void test() {

System.out.println("str " + getA());
setValue("bb ", this::setA);
System.out.println("str " + getA());

System.out.println("In " + getB());
setValue(78, this::setB);
System.out.println("In " + getB());

System.out.println("Lo " + getC());
setValue(78L, this::setC);
System.out.println("Lo " + getC());

System.out.println("bo " + isD());
setValue(false, this::setD);
System.out.println("bo " + isD());

/* result
str 1
str bb
In 0
In 78
Lo 9
Lo 78
bo true
bo false
*/
}

public void set(String str, Consumer<String> function) {
function.accept(str);
}

public void setA(String a) {
this.a = a;
}

public String getA() {
return a;
}

// 设置参数
private <T> void setValue(T value, Consumer<T> consumer) {

consumer.accept(value);

}
}