Optional
A container object which may or may not contain a non-null value. If a value is present, isPresent() returns true. If no value is present, the object is considered empty and isPresent() returns false.
Additional methods that depend on the presence or absence of a contained value are provided, such as .orElse (returns a default value if no value is present) and .ifPresent (performs an action if a value is present).
This is a {@docRoot}/java.base/java/lang/doc-files/ValueBased.html class; programmers should treat instances that are .equals as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
Since
1.8
Parameters
Properties
Functions
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
If a value is present, returns the value, otherwise throws NoSuchElementException.
If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.