An Optional<T>
object is a wrapper for either an object of type T
or no object.
In the former case, we say that the value is present. The Optional<T>
type is intended as a safer alternative for a
reference of type T
that either refers to an object or is null.
You can get an empty optional object using the static factory method Optional.empty()
Optional<String> str = Optional.empty();
You can also create an optional from a non-null value with the static factory method Optional.of(...)
Optional<String> opStr = Optional.of("some string");
When using Optional.of(...)
, the object passed in must not be null or a NPE will be thrown
You can create an Optional
object that may hold a null by using the static factory method Optional.ofNullable(...)
Optional<String> opStr = OPtional.ofNullable(null);
In the example above, the resulting Optional
object would be empty
-
Optional.get()
It returns the wrapped value if present but throws aNoSuchElementException
otherwise -
Optional.orElse(T other)
Allows you to provide a default value for when the optional does not contain a value -
Optional.orElseGet(Supplier<? extends T> other)
TheSupplier
is invoked only if the optional contains no value. -
Optional.orElseThrow(Supplier<? extends X> exceptionSupplier)
Similar toget()
in that it throws an exception when empty, but in this case you can choose the type of exception -
Optional.ifPresent(Consumer<? super T> consumer)
Lets you execute the action given as an argument if a value if present; otherwise no action is taken -
Optional.isPresent()
Returns true is theOptional
contains a value,false
otherwise -
Optional.isEmpty()
Returns true if theOptional
id empty, otherwise it returns false
You can convert an Optional
into a stream using the Optional.stream()
method. If the Optional
contains a value,
it will return a Stream
containing only that value, otherwise it will return an empty stream
It can be helpful to remove empty optionals and keep only the ones with values. In Java 9 you can use the flatMap()
to remove empty optionals
List<String> list - stream.flatMap(Optional::stream).collect(Collectors.toList());
In the example above where the variable stream
is a stream of optionals, the flatMap calls Optional.stream()
on each optional. As described above, this will either return an empty stream or a stream containing only the value
of the optional before finally flattening the stream of streams to produce a stream which contains only optionals
with a value
Previous | Next |
---|