Optional

class Optional<T>

A container object which may or may not contain a non-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 {@docRoot}/java.base/java/lang/doc-files/ValueBased.html class; programmers should treat instances that are .equals as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Since

1.8

Parameters

the type of value

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
open operator override fun equals(obj: Any?): Boolean

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

Link copied to clipboard
fun filter(predicate: (T?) -> Boolean): Optional<T?>

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

Link copied to clipboard
fun <U> flatMap(mapper: (T?) -> Optional<U?>?): Optional<U?>

If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional.

Link copied to clipboard
fun get(): T

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

Link copied to clipboard
open override fun hashCode(): Int

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

Link copied to clipboard
fun ifPresent(action: (T) -> Unit)

If a value is present, performs the given action with the value, otherwise does nothing.

Link copied to clipboard
fun ifPresentOrElse(action: (T) -> Unit, emptyAction: () -> Unit)

If a value is present, performs the given action with the value, otherwise performs the given empty-based action.

Link copied to clipboard
fun <U> map(mapper: (T?) -> U?): Optional<U?>

If a value is present, returns an Optional describing (as if by ofNullable) the result of applying the given mapping function to the value, otherwise returns an empty Optional.

Link copied to clipboard
fun or(supplier: () -> Optional<T?>): Optional<T?>

If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.

Link copied to clipboard
fun orElse(other: T?): T?

If a value is present, returns the value, otherwise returns other.

Link copied to clipboard
fun orElseGet(supplier: () -> T?): T?

If a value is present, returns the value, otherwise returns the result produced by the supplying function.

Link copied to clipboard
fun orElseThrow(): T

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

fun <X : Throwable> orElseThrow(exceptionSupplier: () -> X): T

If a value is present, returns the value, otherwise throws an exception produced by the exception supplying function.

Link copied to clipboard
fun stream(): Sequence<T?>

If a value is present, returns a sequential Sequence containing only that value, otherwise returns an empty Sequence.

Link copied to clipboard
open override 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.