T - the type of the inner valuepublic class Optional<T> extends Object
| Modifier and Type | Method and Description |
|---|---|
static <T> Optional<T> |
empty()
Returns an empty
Optional. |
boolean |
equals(Object obj) |
Optional<T> |
filter(Predicate<? super T> predicate)
Performs filtering on inner value if present.
|
<U> Optional<U> |
flatMap(Function<? super T,Optional<U>> mapper)
Invokes mapping function with
Optional result if value is present. |
T |
get()
Returns inner value if present, otherwise throws
NoSuchElementException. |
int |
hashCode() |
void |
ifPresent(Consumer<? super T> consumer)
Invokes consumer function with value if present.
|
boolean |
isPresent()
Checks value present.
|
<U> Optional<U> |
map(Function<? super T,? extends U> mapper)
Invokes mapping function on inner value if present.
|
static <T> Optional<T> |
of(T value)
Returns an
Optional with the specified present non-null value. |
static <T> Optional<T> |
ofNullable(T value)
Returns an
Optional with the specified value, or empty Optional if value is null. |
T |
orElse(T other)
Returns inner value if present, otherwise returns
other. |
T |
orElseGet(Supplier<? extends T> other)
Returns inner value if present, otherwise returns value produced by supplier function.
|
<X extends Throwable> |
orElseThrow(Supplier<? extends X> exc)
Returns inner value if present, otherwise throws the exception provided by supplier function.
|
String |
toString() |
public static <T> Optional<T> of(T value)
Optional with the specified present non-null value.T - the type of valuevalue - the value to be present, must be non-nullOptionalNullPointerException - if value is nullofNullable(java.lang.Object)public static <T> Optional<T> ofNullable(T value)
Optional with the specified value, or empty Optional if value is null.T - the type of valuevalue - the value which can be nullOptionalof(java.lang.Object)public static <T> Optional<T> empty()
Optional.T - the type of valueOptionalpublic T get()
NoSuchElementException.OptionalNoSuchElementException - if value is not presentpublic boolean isPresent()
true if value present, false otherwisepublic void ifPresent(Consumer<? super T> consumer)
consumer - consumer functionpublic Optional<T> filter(Predicate<? super T> predicate)
predicate - a predicate functionOptional with value if present and matches predicate, otherwise an empty Optionalpublic <U> Optional<U> map(Function<? super T,? extends U> mapper)
U - the type of result valuemapper - mapping functionOptional with transformed value if present, otherwise an empty Optionalpublic <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)
Optional result if value is present.U - the type of result valuemapper - mapping functionOptional with transformed value if present, otherwise an empty Optionalpublic T orElse(T other)
other.other - the value to be returned if inner value is not presentotherpublic T orElseGet(Supplier<? extends T> other)
other - supplier function that produced value if inner value is not presentpublic <X extends Throwable> T orElseThrow(Supplier<? extends X> exc) throws X extends Throwable
X - the type of exception to be thrownexc - supplier function that produced exception to be thrownX - if inner value is not presentX extends ThrowableCopyright © 2015. All rights reserved.