Optional is naturally covariant on
this type, so it is safe to cast an Optional<T> to Optional<S> for any
supertype S of T.
null".
A non-null Optional<T> reference can be used as a replacement for a nullable T
reference. It allows you to represent "a T that must be present" and a "a T that
might be absent" as two distinct types in your program, which can aid clarity.
Some uses of this class include
null to indicate that no
value was available
Optional.absent())
null
(though there are several other
approaches to this that should be considered first)
A common alternative to using this class is to find or create a suitable null object for the type in question.
This class is not intended as a direct analogue of any existing "option" or "maybe" construct from other programming environments, though it may bear some similarities.
An instance of this class is serializable if its reference is absent or is a serializable object.
Comparison to java.util.Optional (JDK 8 and higher): A new Optional
class was added for Java 8. The two classes are extremely similar, but incompatible (they cannot
share a common supertype). All known differences are listed either here or with the
relevant methods below.
java.util.Optional is not.
java.util.Optional has the additional methods ifPresent, filter,
flatMap, and orElseThrow.
java.util offers the primitive-specialized versions OptionalInt, OptionalLong and OptionalDouble, the use of which is recommended; Guava does not
have these.
There are no plans to deprecate this class in the foreseeable future. However, we do gently recommend that you prefer the new, standard Java class whenever possible.
See the Guava User Guide article on using Optional.
| Modifier and Type | Field and Description |
|---|---|
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public static <T> Optional | |
| public abstract Set | |
| public abstract boolean | equals(Object
the reference object with which to compare. object)Overrides java. true if object is an Optional instance, and either the
contained references are equal to each other or both are absent.
|
| public static <T> Optional | fromJavaUtil(Optional<T> javaUtilOptional)
Returns the equivalent |
| public static <T> Optional | fromNullable(T nullableReference)
If |
| public abstract T | |
| public abstract int | |
| public abstract boolean | |
| public static <T> Optional | |
| public abstract T | |
| public abstract Optional | |
| public abstract T | |
| public abstract T | |
| public static <T> Iterable | presentInstances(final Iterable<? extends Optional<? extends T>> optionals)
Returns the value of each present instance from the supplied |
| public static <T> Optional | toJavaUtil(Optional<T> googleOptional)
Returns the equivalent |
| public Optional | |
| public abstract String | |
| public abstract <V> Optional | transform(Function<? super T, V> function)
If the instance is present, it is transformed with the given |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| Optional | back to summary |
|---|---|
| pack-priv Optional() | |
| absent | back to summary |
|---|---|
| public static <T> Optional Returns an Comparison to | |
| asSet | back to summary |
|---|---|
| public abstract Set Returns an immutable singleton Comparison to
... can be replaced with:
Java 9 users: some use cases can be written with calls to
| |
| equals | back to summary |
|---|---|
| public abstract boolean equals(Object object) Overrides java. Returns Comparison to
| |
| fromJavaUtil | back to summary |
|---|---|
| public static <T> Optional Returns the equivalent
| |
| fromNullable | back to summary |
|---|---|
| public static <T> Optional If Comparison to | |
| get | back to summary |
|---|---|
| public abstract T get() Returns the contained instance, which must be present. If the instance might be absent, use
Comparison to
| |
| hashCode | back to summary |
|---|---|
| public abstract int hashCode() Overrides java. Returns a hash code for this instance. Comparison to
| |
| isPresent | back to summary |
|---|---|
| public abstract boolean isPresent() Returns Comparison to | |
| of | back to summary |
|---|---|
| public static <T> Optional Returns an Comparison to
| |
| or | back to summary |
|---|---|
| public abstract T or(T defaultValue) Returns the contained instance if it is present; Note about generics: The signature
As a workaround, it is always safe to cast an
Comparison to | |
| or | back to summary |
|---|---|
| public abstract Optional Returns this Comparison to | |
| or | back to summary |
|---|---|
| public abstract T or(Supplier<? extends T> supplier) Returns the contained instance if it is present; Comparison to
| |
| orNull | back to summary |
|---|---|
| public abstract T orNull() Returns the contained instance if it is present; Comparison to
| |
| presentInstances | back to summary |
|---|---|
| public static <T> Iterable Returns the value of each present instance from the supplied Comparison to Java 9 users: use
| |
| toJavaUtil | back to summary |
|---|---|
| public static <T> Optional Returns the equivalent If Unfortunately, the method reference
| |
| toJavaUtil | back to summary |
|---|---|
| public Optional Returns the equivalent Unfortunately, the method reference
| |
| toString | back to summary |
|---|---|
| public abstract String toString() Overrides java. Returns a string representation for this instance. Comparison to
| |
| transform | back to summary |
|---|---|
| public abstract <V> Optional If the instance is present, it is transformed with the given Comparison to
| |