@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));
}
为空的情况==================================
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