This is a review note of the java.util.Optional class introduced in Java 1.8.
environment
reference
empty / of / ofNullable
Both are static methods. Optional does not have a public constructor, so use these methods to create an Optional object.
empty
Returns an Optional object with a null value.
Optional<String> empty = Optional.empty();
empty.isPresent();
// false
empty.orElse("other");
// other
empty.get();
// java.util.NoSuchElementException: No value present
of
Returns an Optional object with a non-null value.
String str = "nonNull";
Optional<String> nonNullString = Optional.of(str);
nonNullString.isPresent();
// true
nonNullString.get();
// result : nonNull
Passing a null object to of throws an exception.
String str = null;
Optional.of(str);
// java.lang.NullPointerException
ofNullable
Returns an Optional object with a value that may be null (nullable).
String str = null;
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.isPresent();
// false
nullableString.orElse("other");
// other
get / isPresent
get
Returns the value held by the Optional object. If the value to be retained is null, NoSuchElementException will be thrown.
String str = "apple";
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.get();
// apple
String str = null;
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.get();
// java.util.NoSuchElementException: No value present
isPresent
Returns true if the value held by the Optional object is non-null, false if it is null.
String str = "apple";
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.isPresent();
// true
String str = null;
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.isPresent();
// false
orElse / orElseGet / orElseThrow
orElse
Returns the value held by the Optional object. If the value to be retained is null, the specified value is returned. (Null is also possible)
String str = "nonNull";
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.orElse("other");
// nonNull
String str = null;
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.orElse("other");
// other
nullableString.orElse(null);
// null
orElseGet
Returns the value held by the Optional object. If the value to be retained is null, the result of the specified Supplier is returned. If Supplier returns null, it will return null.
String str = "nonNull";
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.orElseGet(() -> "other");
// nonNull
String str = null;
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.orElseGet(() -> "other");
// other
nullableString.orElseGet(() -> null);
// null
orElseThrow
Returns the value held by the Optional object. If the value to be retained is null, the specified exception will be thrown.
String str = null;
Optional<String> nullableString = Optional.ofNullable(str);
nullableString.orElseThrow(RuntimeException::new);
// java.lang.RuntimeException
ifPresent
If the value held by the Optional object is non-null, the Consumer processing block specified in the argument is executed with that value. If the value held by the Optional object is null, no exception will be thrown and nothing will be executed.
Optional<Employee> employee = Optional.ofNullable(new Employee(1L, "john"));
employee.ifPresent((e) -> {
e.setName(e.getName().toUpperCase());
System.out.println(e);
// Employee{id=1, name='JOHN'}
});
filter / map / flatMap
filter
If the value held by the Optional object is non-null, the Predicate processing block specified in the argument with that value is executed.
Optional<String> nullableString = Optional.ofNullable("banana");
Optional<String> filtering = nullableString.filter(str -> str.length() > 5);
filtering.isPresent();
// true
filtering.orElse("other");
// banana
If the value held by the Optional object is null, or if the Predicate processing block returns false, an empty Optional is returned.
Optional<String> nullableString = Optional.ofNullable("apple");
// Optional<String> nullableString = Optional.ofNullable(null);
Optional<String> filtering = nullableString.filter(str -> str.length() > 5);
filtering.isPresent();
// false
filtering.orElse("other");
// other
map
If the value held by the Optional object is non-null, the processing block of the Function specified in the argument is executed with that value.
Optional<String> nullableString = Optional.ofNullable("apple");
Optional<Integer> mapping = nullableString.map(str -> str.length());
mapping.isPresent();
// true
mapping.orElse(0);
// 5
If the value held by the Optional object is null, or if the processing block of the Function returns null, an empty Optional is returned.
Optional<String> nullableString = Optional.ofNullable(null);
Optional<Integer> mapping = nullableString.map(str -> str.length());
mapping.isPresent();
// false
mapping.orElse(0);
// 0
flatMap
If the value held by the Optional object is non-null, the processing block of the Function specified in the argument is executed with that value. The difference from map is that Function returns an Optional object.
The Function specified in map takes T type and returns U type.
Function<? super T,? extends U> mapper
The Function specified in flatMap takes T type and returns Optional type.
Function<? super T,Optional<U>> mapper
Optional<Long> optId = Optional.of(1L);
Optional<String> optName = Optional.of("john");
Optional<Employee> result = optId.flatMap(id -> optName.flatMap(name -> Optional.of(new Employee(id, name))));
result.isPresent();
// true
result.orElse(null);
// Employee{id=1, name='john'}
If the value held by the Optional object is null, an empty Optional is returned.
Optional<Long> optId = Optional.of(1L);
Optional<String> optName = Optional.ofNullable(null);
Optional<Employee> result = optId.flatMap(id -> optName.flatMap(name -> Optional.of(new Employee(id, name))));
result.isPresent();
// false
result.orElse(null);
// null
If you use a Function that returns Optional in map, the return value will be an unexpected type.
Optional<Optional<String>> nullableName = employee.map(emp -> Optional.ofNullable(emp.getName()));
ifPresentOrElse
There are more variations of ifPresent. If the value held by the Optional object is null, the Runnable processing block specified in the argument is executed.
Optional<Employee> employee = Optional.ofNullable(null);
employee.ifPresentOrElse((e) -> {
e.setName(e.getName().toUpperCase());
System.out.println(e);
},
() -> {
System.out.println("empty action");
// empty action
});
or
If the value held by the Optional object is null, the Optional object generated by the Supplier specified in the argument is returned. An exception will be thrown if the Supplier returns null.
String str = null;
Optional<String> nonNullString = Optional.ofNullable(str).or(() -> Optional.of("nonNull"));
nonNullString.isPresent();
// true
nonNullString.get();
// nonNull
stream
If the value held by the Optional object is non-null, a stream with only that value is returned, and if it is null, an empty stream is returned.
List<Optional<String>> list = List.of(
Optional.of("a"),
Optional.of("b"),
Optional.of("c"),
Optional.empty(),
Optional.of("e"),
Optional.empty(),
Optional.of("g")
);
list.stream().filter(Optional::isPresent).map(Optional::get).forEach(System.out::println);
// a
// b
// c
// e
// g
//When using stream
list.stream().flatMap(Optional::stream).forEach(System.out::println);
// a
// b
// c
// e
// g
orElseThrow
Returns the value held by the Optional object. Throws NoSuchElementException if the value to keep is null. This behavior is the same as the get method. For this reason, rather than adding a variation of orElseThrow with the same method name from Java 1.8
java1.8_orElseThrow signature
public <X extends Throwable> T orElseThrow​(Supplier<? extends X> exceptionSupplier)
throws X extends Throwable
It feels like a method that replaces the get method, which removes ambiguity from the get method name.
java1.8_get signature
public T get()
You can see that orElseThrow can throw an exception from the method name.
java10_orElseThrow signature
public T orElseThrow()
isEmpty
java11_isEmpty signature
public boolean isEmpty()
Returns true if the value does not exist.
No API has been added.
No API has been added.
No API has been added.
Recommended Posts