hutool的Opt工具类,非空判断

hutool的Opt工具类,非空判断

一月 05, 2022
该文章更新于 2022.01.05

一、Hutool🐂🍺:Opt (hutool-码云(gitee.com))

二、我的demo

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
@Test
void testHutool() {
List<String> objects = new ArrayList<>();

/*
* 为空的情况
* */
System.out.println("\n\n\n为空的情况==================================");
System.out.println("Opt.empty() = " + Opt.empty());
System.out.println("Opt.of(objects) = " + Opt.of(objects));
System.out.println("Opt.ofNullable(objects) = " + Opt.ofNullable(objects));
System.out.println("Opt.ofBlankAble(objects) = " + Opt.ofBlankAble(objects));
System.out.println("Opt.ofEmptyAble(objects) = " + Opt.ofEmptyAble(objects));
System.out.println("objects.isEmpty() = " + objects.isEmpty());
Opt.ofEmptyAble(objects).ifPresent(strings -> System.out.println("strings = " + strings));
Opt.ofEmptyAble(objects).ifPresentOrElse(Console::log, () -> Console.error("传入的值不存在呢"));
System.out.println("Opt.ofEmptyAble(objects).filter(strings -> strings.size() > 10) = " + Opt.ofEmptyAble(objects).filter(strings -> strings.size() > 10));
Opt.ofEmptyAble(objects).peek(System.out::println);
Opt.ofEmptyAble(objects).peeks(strings -> System.out.println("strings.size() = " + strings.size()));



/*
不为空的情况
*/
System.out.println("\n\n\n不为空的情况==================================");
objects.add("张三");
objects.add("李四");
objects.add("王五");
objects.add("赵六");
objects.add(null);
objects.add(" ");
System.out.println("Opt.of(objects) = " + Opt.of(objects));
System.out.println("Opt.ofNullable(objects) = " + Opt.ofNullable(objects));
System.out.println("Opt.ofBlankAble(objects) = " + Opt.ofBlankAble(objects));
System.out.println("Opt.ofEmptyAble(objects) = " + Opt.ofEmptyAble(objects));
System.out.println("Opt.ofEmptyAble(objects).get() = " + Opt.ofEmptyAble(objects).get());
System.out.println("objects.isEmpty() = " + objects.isEmpty());
Opt.ofEmptyAble(objects).ifPresent(strings -> System.out.println("strings = " + strings));
Opt.ofEmptyAble(objects).ifPresentOrElse(Console::log, () -> Console.error("传入的值不存在呢"));
System.out.println("Opt.ofEmptyAble(objects).filter(strings -> strings.size() > 10) = " + Opt.ofEmptyAble(objects).filter(strings -> strings.size() > 3));
Opt.ofEmptyAble(objects).peek(System.out::println);
Opt.ofEmptyAble(objects).peeks(strings -> System.out.println("strings.size() = " + strings.size()));



/*
其他东西
*/
System.out.println("\n\n\n其他东西===========================");
int num1 = 100;
int num2 = 11;
System.out.println("Opt.ofTry(() -> num1 + num2) = " + Opt.ofTry(() -> num1 + num2));
}

三、运行结果:

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
为空的情况==================================
Opt.empty() = null
Opt.of(objects) = []
Opt.ofNullable(objects) = []
Opt.ofBlankAble(objects) = []
Opt.ofEmptyAble(objects) = null
objects.isEmpty() = true
传入的值不存在呢
Opt.ofEmptyAble(objects).filter(strings -> strings.size() > 10) = null



不为空的情况==================================
Opt.of(objects) = [张三, 李四, 王五, 赵六, null, ]
Opt.ofNullable(objects) = [张三, 李四, 王五, 赵六, null, ]
Opt.ofBlankAble(objects) = [张三, 李四, 王五, 赵六, null, ]
Opt.ofEmptyAble(objects) = [张三, 李四, 王五, 赵六, null, ]
Opt.ofEmptyAble(objects).get() = [张三, 李四, 王五, 赵六, null, ]
objects.isEmpty() = false
strings = [张三, 李四, 王五, 赵六, null, ]
[张三, 李四, 王五, 赵六, null, ]
Opt.ofEmptyAble(objects).filter(strings -> strings.size() > 10) = [张三, 李四, 王五, 赵六, null, ]
[张三, 李四, 王五, 赵六, null, ]
strings.size() = 6



其他东西===========================
Opt.ofTry(() -> num1 + num2) = 111