1、of方法用于创建一个非空Optional对象。如果参数为null,则抛出NullPointerException异常。
Optional<String> optionalStr = Optional.of("Hello World");
System.out.println(optionalStr); // Optional[Hello World]
Optional<String> optionalStr1 = Optional.of("null");
System.out.println(optionalStr1); // Optional[null]
// 如果参数为null,则抛出NullPointerException异常。
//Optional<String> optionalStr2 = Optional.of(null);
//System.out.println(optionalStr2);
2、ofNullable方法用于创建一个可能为空的Optional对象.
String str = null;
Optional<String> optionalStr3 = Optional.ofNullable(str);
System.out.println(optionalStr3); // Optional.empty
System.out.println(Optional.empty()); // Optional.empty
3、isPresent方法返回一个布尔值,表示Optional对象中是否包含非空值。
Optional<String> optionalStr4 = Optional.ofNullable("Hello World");
if (optionalStr4.isPresent()) {
System.out.println("The value is present.");
} else {
System.out.println("The value is absent.");
} // The value is present.
Optional<String> optionalStr5 = Optional.empty();
if (optionalStr5.isPresent()) {
System.out.println("The value is present.");
} else {
System.out.println("The value is absent.");
} // The value is absent.
4、get方法返回Optional对象中的值。如果Optional对象为空,则抛出NoSuchElementException异常。
Optional<String> optionalStr6 = Optional.ofNullable("Hello World");
String str6 = optionalStr6.get();
System.out.println(str6); // Hello World
Optional<String> optionalStr7 = Optional.empty();
//String str7 = optionalStr7.get(); // Exception in thread "main" java.util.NoSuchElementException: No value present
//System.out.println(str7);
5、orElse方法在Optional对象为空时提供备选值.
Optional<String> optionalStr8 = Optional.empty();
String str8 = optionalStr8.orElse("Default Message");
System.out.println(str8); // Default Message
Optional<String> optionalStr9 = Optional.ofNullable("Hello World");
String str9 = optionalStr9.orElse("Default Message");
System.out.println(str9); // Hello World
6、ifPresent方法允许在Optional对象中存在非空值时执行一些操作。
Optional<String> optionalStr10 = Optional.ofNullable("Hello World");
optionalStr10.ifPresent(str0 -> System.out.println("The value is present: " + str0)); // The value is present: Hello World
Optional<String> optionalStr11 = Optional.empty();
optionalStr11.ifPresent(str0 -> System.out.println("The value is present: " + str0)); //
7、filter方法允许过滤Optional对象中的值。如果值满足Predicate函数式接口所定义的条件,则返回一个包含该值的Optional对象;否则返回空的Optional对象.
Optional<String> optionalStr12 = Optional.ofNullable("Hello World");
Optional<String> filtered12 = optionalStr12.filter(str0 -> str0.contains("World"));
if (filtered12.isPresent()) {
System.out.println(filtered12.get()); // Hello World
} else {
System.out.println("The value does not contain 'World'.");
}
Optional<String> optionalStr13 = Optional.empty();
Optional<String> filtered13 = optionalStr13.filter(str0 -> str0.contains("World"));
if (filtered13.isPresent()) {
System.out.println(filtered13.get());
} else {
System.out.println("The value does not contain 'World'."); // The value does not contain 'World'.
}
8、map方法允许将Optional对象中的值转换为另一种类型并返回一个包含新值的Optional对象。
Optional<String> optionalStr14 = Optional.ofNullable("Hello World");
Optional<Integer> length14 = optionalStr14.map(String::length);
System.out.println(length14.get()); // 11
Optional<String> optionalStr15 = Optional.empty();
// Optional<Integer> length15 = optionalStr15.map(String::length);
// System.out.println(length15.get()); // Exception in thread "main" java.util.NoSuchElementException: No value present
9、flatMap方法与map方法类似,但它接受一个返回Optional对象的Function函数式接口作为参数,并将其返回值作为最终结果。可以在Optional对象上执行更复杂的操作。
Optional<String> optionalStr16 = Optional.ofNullable("Hello World");
Optional<Character> firstChar16 = optionalStr16.flatMap(str0 -> Optional.ofNullable(str0.charAt(0)));
System.out.println(firstChar16.get()); // H
Optional<String> optionalStr17 = Optional.empty();
//Optional<Character> firstChar17 = optionalStr17.flatMap(str0 -> Optional.ofNullable(str0.charAt(0)));
//System.out.println(firstChar17.get()); // Exception in thread "main" java.util.NoSuchElementException: No value present
10、orElseGet方法与orElse方法类似,但它接受一个Supplier函数式接口作为参数,该接口定义了在Optional对象为空时提供备选值的逻辑。
Optional<String> optionalStr18 = Optional.ofNullable(null);
String str18 = optionalStr18.orElseGet(() -> "Default Message");
System.out.println(str18); // Default Message
// orElseGet方法与orElse方法类似,但它接受一个Supplier函数式接口作为参数,该接口定义了在Optional对象为空时提供备选值的逻辑。
Optional<String> optionalStr19 = Optional.ofNullable(null);
String str19 = optionalStr19.orElseGet(() -> "Default Message");
System.out.println(str19); // Default Message
11、orElseThrow方法在Optional对象为空时抛出指定类型的异常.
Optional<String> optionalStr20 = Optional.ofNullable(null);
String str20 = optionalStr20.orElseThrow(() -> new RuntimeException("Value is null")); // Exception in thread "main" java.lang.RuntimeException: Value is null
文章来源地址https://www.toymoban.com/news/detail-417771.html
文章来源:https://www.toymoban.com/news/detail-417771.html
到了这里,关于Optional 类 - Java 8的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!