Google is committed to advancing racial equity for Black communities. See how.
Added in API level 24

Optional

class Optional<T : Any!>
kotlin.Any
   ↳ java.util.Optional

A container object which may or may not contain a non-null value. If a value is present, 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).

Summary

Public methods

static Optional<T>

Returns an empty Optional instance.

Boolean
equals(other: Any?)

Indicates whether some other object is "equal to" this Optional.

Optional<T>!
filter(predicate: Predicate<in T>!)

If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.

Optional<U>!
flatMap(mapper: Function<in T, Optional<U>!>!)

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

Returns the hash code value of the present value, if any, or 0 (zero) if no value is present.

Unit
ifPresent(consumer: Consumer<in T>)

If a value is present, invoke the specified consumer with the value, otherwise do nothing.

Boolean

Return true if there is a value present, otherwise false.

Optional<U>!
map(mapper: Function<in T, out U>!)

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.

static Optional<T>
of(value: T)

Returns an Optional with the specified present non-null value.

static Optional<T>
ofNullable(value: T?)

Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.

T
orElse(other: T)

Return the value if present, otherwise return other.

T
orElseGet(other: Supplier<out T>!)

Return the value if present, otherwise invoke other and return the result of that invocation.

T
orElseThrow(exceptionSupplier: Supplier<out X>!)

Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

String

Returns a non-empty string representation of this Optional suitable for debugging.

Public methods

empty

Added in API level 24
static fun <T : Any!> empty(): Optional<T>

Returns an empty Optional instance. No value is present for this Optional.

Parameters
<T> Type of the non-existent value
Return
Optional<T> an empty Optional

equals

Added in API level 24
fun equals(other: Any?): Boolean

Indicates whether some other object is "equal to" this Optional. The other object is considered equal if:

  • it is also an Optional and;
  • both instances have no value present or;
  • the present values are "equal to" each other via equals().

Parameters
obj an object to be tested for equality
Return
Boolean {code true} if the other object is "equal to" this object otherwise false

filter

Added in API level 24
fun filter(predicate: Predicate<in T>!): Optional<T>!

If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.

Parameters
predicate Predicate<in T>!: a predicate to apply to the value, if present
Return
Optional<T>! an Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional
Exceptions
java.lang.NullPointerException if the predicate is null

flatMap

Added in API level 24
fun <U : Any!> flatMap(mapper: Function<in T, Optional<U>!>!): Optional<U>!

If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional. This method is similar to map(java.util.function.Function), but the provided mapper is one whose result is already an Optional, and if invoked, flatMap does not wrap it with an additional Optional.

Parameters
<U> The type parameter to the Optional returned by
mapper Function<in T, Optional<U>!>!: a mapping function to apply to the value, if present the mapping function
Return
Optional<U>! the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
Exceptions
java.lang.NullPointerException if the mapping function is null or returns a null result

get

Added in API level 24
fun get(): T

If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.

Return
T the non-null value held by this Optional
Exceptions
java.util.NoSuchElementException if there is no value present

hashCode

Added in API level 24
fun hashCode(): Int

Returns the hash code value of the present value, if any, or 0 (zero) if no value is present.

Return
Int hash code value of the present value or 0 if no value is present

ifPresent

Added in API level 24
fun ifPresent(consumer: Consumer<in T>): Unit

If a value is present, invoke the specified consumer with the value, otherwise do nothing.

Parameters
consumer Consumer<in T>: block to be executed if a value is present
Exceptions
java.lang.NullPointerException if value is present and consumer is null

isPresent

Added in API level 24
fun isPresent(): Boolean

Return true if there is a value present, otherwise false.

Return
Boolean true if there is a value present, otherwise false

map

Added in API level 24
fun <U : Any!> map(mapper: Function<in T, out U>!): Optional<U>!

If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.

Parameters
<U> The type of the result of the mapping function
mapper Function<in T, out U>!: a mapping function to apply to the value, if present
Return
Optional<U>! an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
Exceptions
java.lang.NullPointerException if the mapping function is null

of

Added in API level 24
static fun <T : Any!> of(value: T): Optional<T>

Returns an Optional with the specified present non-null value.

Parameters
<T> the class of the value
value T: the value to be present, which must be non-null
Return
Optional<T> an Optional with the value present
Exceptions
java.lang.NullPointerException if value is null

ofNullable

Added in API level 24
static fun <T : Any!> ofNullable(value: T?): Optional<T>

Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.

Parameters
<T> the class of the value
value T?: the possibly-null value to describe
Return
Optional<T> an Optional with a present value if the specified value is non-null, otherwise an empty Optional

orElse

Added in API level 24
fun orElse(other: T): T

Return the value if present, otherwise return other.

Parameters
other T: the value to be returned if there is no value present, may be null
Return
T the value, if present, otherwise other

orElseGet

Added in API level 24
fun orElseGet(other: Supplier<out T>!): T

Return the value if present, otherwise invoke other and return the result of that invocation.

Parameters
other Supplier<out T>!: a Supplier whose result is returned if no value is present
Return
T the value if present otherwise the result of other.get()
Exceptions
java.lang.NullPointerException if value is not present and other is null

orElseThrow

Added in API level 24
fun <X : Throwable!> orElseThrow(exceptionSupplier: Supplier<out X>!): T

Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.

Parameters
<X> Type of the exception to be thrown
exceptionSupplier Supplier<out X>!: The supplier which will return the exception to be thrown
Return
T the present value
Exceptions
X if there is no value present
java.lang.NullPointerException if no value is present and exceptionSupplier is null

toString

Added in API level 24
fun toString(): String

Returns a non-empty string representation of this Optional suitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.

Return
String the string representation of this instance