Class Optional<T>
- Type Parameters:
T- the type of instance that can be contained.Optionalis naturally covariant on this type, so it is safe to cast anOptional<T>toOptional<S>for any supertypeSofT.
- All Implemented Interfaces:
Serializable
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
- As a method return type, as an alternative to returning
nullto indicate that no value was available - To distinguish between "unknown" (for example, not present in a map) and "known to
have no value" (present in the map, with value
Optional.absent()) - To wrap nullable references for storage in a collection that does not support
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.
See the Guava User Guide article on
using Optional.
- Since:
- 10.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T> absent()Deprecated.Returns anOptionalinstance with no contained reference.asSet()Deprecated.abstract booleanDeprecated.Returnstrueifobjectis anOptionalinstance, and either the contained references are equal to each other or both are absent.static <T> Optional<T> fromNullable(T nullableReference) Deprecated.IfnullableReferenceis non-null, returns anOptionalinstance containing that reference; otherwise returnsabsent().abstract Tget()Deprecated.Returns the contained instance, which must be present.abstract inthashCode()Deprecated.Returns a hash code for this instance.abstract booleanDeprecated.Returnstrueif this holder contains a (non-null) instance.static <T> Optional<T> of(T reference) Deprecated.Returns anOptionalinstance containing the given non-null reference.Deprecated.Returns thisOptionalif it has a value present;secondChoiceotherwise.abstract TDeprecated.Returns the contained instance if it is present;supplier.get()otherwise.abstract TDeprecated.Returns the contained instance if it is present;defaultValueotherwise.abstract TorNull()Deprecated.Returns the contained instance if it is present;nullotherwise.static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>> optionals) Deprecated.Returns the value of each present instance from the suppliedoptionals, in order, skipping over occurrences ofabsent().abstract StringtoString()Deprecated.Returns a string representation for this instance.abstract <V> Optional<V>
-
Method Details
-
absent
Deprecated.Returns anOptionalinstance with no contained reference. -
of
Deprecated.Returns anOptionalinstance containing the given non-null reference. -
fromNullable
Deprecated.IfnullableReferenceis non-null, returns anOptionalinstance containing that reference; otherwise returnsabsent(). -
isPresent
public abstract boolean isPresent()Deprecated.Returnstrueif this holder contains a (non-null) instance. -
get
Deprecated.Returns the contained instance, which must be present. If the instance might be absent, useor(Object)ororNull()instead.- Throws:
IllegalStateException- if the instance is absent (isPresent()returnsfalse)
-
or
Deprecated.Returns the contained instance if it is present;defaultValueotherwise. If no default value should be required because the instance is known to be present, useget()instead. For a default value ofnull, useorNull().Note about generics: The signature
public T or(T defaultValue)is overly restrictive. However, the ideal signature,public <S super T> S or(S), is not legal Java. As a result, some sensible operations involving subtypes are compile errors:Optional<Integer> optionalInt = getSomeOptionalInt(); Number value = optionalInt.or(0.5); // error FluentIterable<? extends Number> numbers = getSomeNumbers(); Optional<? extends Number> first = numbers.first(); Number value = first.or(0.5); // errorAs a workaround, it is always safe to cast an
Optional<? extends T>toOptional<T>. Casting either of the above exampleOptionalinstances toOptional<Number>(whereNumberis the desired output type) solves the problem:Optional<Number> optionalInt = (Optional) getSomeOptionalInt(); Number value = optionalInt.or(0.5); // fine FluentIterable<? extends Number> numbers = getSomeNumbers(); Optional<Number> first = (Optional) numbers.first(); Number value = first.or(0.5); // fine -
or
Deprecated.Returns thisOptionalif it has a value present;secondChoiceotherwise. -
or
Deprecated.Returns the contained instance if it is present;supplier.get()otherwise. If the supplier returnsnull, aNullPointerExceptionis thrown.- Throws:
NullPointerException- if the supplier returnsnull
-
orNull
Deprecated.Returns the contained instance if it is present;nullotherwise. If the instance is known to be present, useget()instead. -
asSet
Deprecated.Returns an immutable singletonSetwhose only element is the contained instance if it is present; an empty immutableSetotherwise.- Since:
- 11.0
-
transform
Deprecated.If the instance is present, it is transformed with the givenFunction; otherwise,absent()is returned. If the function returnsnull, aNullPointerExceptionis thrown.- Throws:
NullPointerException- if the function returnsnull- Since:
- 12.0
-
equals
Deprecated.Returnstrueifobjectis anOptionalinstance, and either the contained references are equal to each other or both are absent. Note thatOptionalinstances of differing parameterized types can be equal. -
hashCode
public abstract int hashCode()Deprecated.Returns a hash code for this instance. -
toString
Deprecated.Returns a string representation for this instance. The form of this string representation is unspecified. -
presentInstances
@Beta public static <T> Iterable<T> presentInstances(Iterable<? extends Optional<? extends T>> optionals) Deprecated.Returns the value of each present instance from the suppliedoptionals, in order, skipping over occurrences ofabsent(). Iterators are unmodifiable and are evaluated lazily.- Since:
- 11.0 (generics widened in 13.0)
-