public final class Optional<T> extends Object
isPresent()
will return true
and
get()
will return the value.
Additional methods that depend on the presence or absence of a contained
value are provided, such as orElse()
(return a default value if value not present) and
ifPresent()
(execute a block
of code if the value is present).
This is a value-based
class; use of identity-sensitive operations (including reference equality
(==
), identity hash code, or synchronization) on instances of
Optional
may have unpredictable results and should be avoided.
Modifier and Type | Method | Description |
---|---|---|
static <T> Optional<T> |
empty() |
Returns an empty
Optional instance. |
boolean |
equals(Object obj) |
Indicates whether some other object is "equal to" this Optional.
|
Optional<T> |
filter(Predicate<? super T> predicate) |
If a value is present, and the value matches the given predicate,
return an
Optional describing the value, otherwise return an
empty Optional . |
<U> Optional<U> |
flatMap(Function<? super T,Optional<U>> mapper) |
If a value is present, apply the provided
Optional -bearing
mapping function to it, return that result, otherwise return an empty
Optional . |
T |
get() |
If a value is present in this
Optional , returns the value,
otherwise throws NoSuchElementException . |
int |
hashCode() |
Returns the hash code value of the present value, if any, or 0 (zero) if
no value is present.
|
void |
ifPresent(Consumer<? super T> consumer) |
If a value is present, invoke the specified consumer with the value,
otherwise do nothing.
|
boolean |
isPresent() |
Return
true if there is a value present, otherwise false . |
<U> Optional<U> |
|