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 value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
API Note
Optional is primarily intended for use as a method return type where
there is a clear need to represent "no result," and where using null
is likely to cause errors. A variable whose type is Optional should
never itself be null; it should always point to an Optional
instance.
| Modifier and Type | Field and Description |
|---|---|
| private static final Optional | EMPTY
Common instance for |
| private final T | value
If non-null, the value; if null, indicates no value is present |
| Access | Constructor and Description |
|---|---|
| private | Optional(T
the value to describe; it's the caller's responsibility to
ensure the value is non- value)null unless creating the singleton
instance returned by empty().Constructs an instance with the described value. |
| Modifier and Type | Method and Description |
|---|---|
| public static < The type of the non-existent value T> Optional | |
| public boolean | Returns: true if the other object is "equal to" this object
otherwise falsean object to be tested for equality obj)Overrides java. Indicates whether some other object is "equal to" this |
| public Optional | Returns: anOptional describing the value of this
Optional, if a value is present and the value matches the
given predicate, otherwise an empty Optionalthe predicate to apply to a value, if present predicate)If a value is present, and the value matches the given predicate,
returns an |
| public < The type of value of the U> OptionalOptional returned by the
mapping function | Returns: the result of applying anOptional-bearing mapping
function to the value of this Optional, if a value is
present, otherwise an empty Optionalthe mapping function to apply to a value, if present mapper)If a value is present, returns the result of applying the given
|
| public T | Returns: the non-null value described by this OptionalIf a value is present, returns the value, otherwise throws
|
| public int | Returns: hash code value of the present value or0 if no value is
presentOverrides java. Returns the hash code of the value, if present, otherwise |
| public void | |
| public void | ifPresentOrElse(Consumer<? super T>
the action to be performed, if a value is present action, Runnable the empty-based action to be performed, if no value is
present emptyAction)If a value is present, performs the given action with the value, otherwise performs the given empty-based action. |
| public boolean | Returns: true if a value is not present, otherwise falseIf a value is not present, returns |
| public boolean | Returns: true if a value is present, otherwise falseIf a value is present, returns |
| public < The type of the value returned from the mapping function U> Optional | Returns: anOptional describing the result of applying a mapping
function to the value of this Optional, if a value is
present, otherwise an empty Optionalthe mapping function to apply to a value, if present mapper)If a value is present, returns an |
| public static < the type of the value T> Optional | Returns: anOptional with the value presentthe value to describe, which must be non- value)nullReturns an |
| public static < the type of the value T> Optional | Returns: anOptional with a present value if the specified value
is non-null, otherwise an empty Optionalthe possibly- value)null value to describeReturns an |
| public Optional | Returns: returns anOptional describing the value of this
Optional, if a value is present, otherwise an
Optional produced by the supplying function.the supplying function that produces an supplier)Optional
to be returnedIf a value is present, returns an |
| public T | Returns: the value, if present, otherwiseotherthe value to be returned, if no value is present.
May be other)null.If a value is present, returns the value, otherwise returns
|
| public T | |
| public T | Returns: the non-null value described by this OptionalIf a value is present, returns the value, otherwise throws
|
| public < Type of the exception to be thrown X extends Throwable> T | Returns: the value, if presentthe supplying function that produces an
exception to be thrown exceptionSupplier)If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. |
| public Stream | |
| public String | Returns: the string representation of this instanceOverrides java. Returns a non-empty string representation of this |
| EMPTY | back to summary |
|---|---|
| private static final Optional<?> EMPTY Common instance for | |
| value | back to summary |
|---|---|
| private final T value If non-null, the value; if null, indicates no value is present | |
| Optional | back to summary |
|---|---|
| private Optional(T value) Constructs an instance with the described value.
| |
| empty | back to summary |
|---|---|
| public static <T> Optional Returns an empty API Note Though it may be tempting to do so, avoid testing if an object is empty
by comparing with
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Indicates whether some other object is "equal to" this
| |
| filter | back to summary |
|---|---|
| public Optional If a value is present, and the value matches the given predicate,
returns an
| |
| flatMap | back to summary |
|---|---|
| public <U> Optional If a value is present, returns the result of applying the given
This method is similar to
| |
| get | back to summary |
|---|---|
| public T get() If a value is present, returns the value, otherwise throws
API Note The preferred alternative to this method is
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Returns the hash code of the value, if present, otherwise
| |
| ifPresent | back to summary |
|---|---|
| public void ifPresent(Consumer<? super T> action) If a value is present, performs the given action with the value, otherwise does nothing.
| |
| ifPresentOrElse | back to summary |
|---|---|
| public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() If a value is not present, returns
| |
| isPresent | back to summary |
|---|---|
| public boolean isPresent() If a value is present, returns
| |
| map | back to summary |
|---|---|
| public <U> Optional If a value is present, returns an If the mapping function returns a API Note This method supports post-processing on
Here, findFirst returns an Optional<URI>, and then
map returns an Optional<Path> for the desired
URI if one exists.
| |
| of | back to summary |
|---|---|
| public static <T> Optional Returns an
| |
| ofNullable | back to summary |
|---|---|
| public static <T> Optional Returns an
| |
| or | back to summary |
|---|---|
| public Optional If a value is present, returns an
| |
| orElse | back to summary |
|---|---|
| public T orElse(T other) If a value is present, returns the value, otherwise returns
| |
| orElseGet | back to summary |
|---|---|
| public T orElseGet(Supplier<? extends T> supplier) If a value is present, returns the value, otherwise returns the result produced by the supplying function.
| |
| orElseThrow | back to summary |
|---|---|
| public T orElseThrow() If a value is present, returns the value, otherwise throws
| |
| orElseThrow | back to summary |
|---|---|
| public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws Supplier-:X If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function. API Note A method reference to the exception constructor with an empty argument
list can be used as the supplier. For example,
| |
| stream | back to summary |
|---|---|
| public Stream If a value is present, returns a sequential API Note This method can be used to transform a
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a non-empty string representation of this Implementation Specification If a value is present the result must include its string representation
in the result. Empty and present | |