Optional (Java Platform SE 8 ) (original) (raw)

Modifier and Type

Method

Description

static <T> [Optional](../../java/util/Optional.html "class in java.util")<T>

[empty](../../java/util/Optional.html#empty--)()

Returns an empty Optional instance.

boolean

[equals](../../java/util/Optional.html#equals-java.lang.Object-)([Object](../../java/lang/Object.html "class in java.lang") obj)

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

[Optional](../../java/util/Optional.html "class in java.util")<[T](../../java/util/Optional.html "type parameter in Optional")>

[filter](../../java/util/Optional.html#filter-java.util.function.Predicate-)([Predicate](../../java/util/function/Predicate.html "interface in java.util.function")<? super [T](../../java/util/Optional.html "type parameter in Optional")> 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](../../java/util/Optional.html "class in java.util")<U>

[flatMap](../../java/util/Optional.html#flatMap-java.util.function.Function-)([Function](../../java/util/function/Function.html "interface in java.util.function")<? super [T](../../java/util/Optional.html "type parameter in Optional"),[Optional](../../java/util/Optional.html "class in java.util")<U>> mapper)

If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an emptyOptional.

[T](../../java/util/Optional.html "type parameter in Optional")

[get](../../java/util/Optional.html#get--)()

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

int

[hashCode](../../java/util/Optional.html#hashCode--)()

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

void

[ifPresent](../../java/util/Optional.html#ifPresent-java.util.function.Consumer-)([Consumer](../../java/util/function/Consumer.html "interface in java.util.function")<? super [T](../../java/util/Optional.html "type parameter in Optional")> consumer)

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

boolean

[isPresent](../../java/util/Optional.html#isPresent--)()

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

<U> [Optional](../../java/util/Optional.html "class in java.util")<U>

[map](../../java/util/Optional.html#map-java.util.function.Function-)([Function](../../java/util/function/Function.html "interface in java.util.function")<? super [T](../../java/util/Optional.html "type parameter in Optional"),? extends U> mapper)

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 <T> [Optional](../../java/util/Optional.html "class in java.util")<T>

[of](../../java/util/Optional.html#of-T-)(T value)

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

static <T> [Optional](../../java/util/Optional.html "class in java.util")<T>

[ofNullable](../../java/util/Optional.html#ofNullable-T-)(T value)

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

[T](../../java/util/Optional.html "type parameter in Optional")

[orElse](../../java/util/Optional.html#orElse-T-)([T](../../java/util/Optional.html "type parameter in Optional") other)

Return the value if present, otherwise return other.

[T](../../java/util/Optional.html "type parameter in Optional")

[orElseGet](../../java/util/Optional.html#orElseGet-java.util.function.Supplier-)([Supplier](../../java/util/function/Supplier.html "interface in java.util.function")<? extends [T](../../java/util/Optional.html "type parameter in Optional")> other)

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

<X extends [Throwable](../../java/lang/Throwable.html "class in java.lang")> [T](../../java/util/Optional.html "type parameter in Optional")

[orElseThrow](../../java/util/Optional.html#orElseThrow-java.util.function.Supplier-)([Supplier](../../java/util/function/Supplier.html "interface in java.util.function")<? extends X> exceptionSupplier)

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

[String](../../java/lang/String.html "class in java.lang")

[toString](../../java/util/Optional.html#toString--)()

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