GeezerVison is showing docs.oracle.com — Optional (Java SE 9 & JDK 9 )
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods
| Modifier and Type |
Method |
Description |
static <T> Optional<T> |
empty() |
Returns an empty Optional instance.
|
boolean |
equals(Object obj) |
Indicates whether some other object is "equal to" this Optional.
|
Optional<T> |
filter(Predicate<? super T> predicate) |
If a value is present, and the value matches the given predicate,
returns an Optional describing the value, otherwise returns an
empty Optional.
|
<U> Optional<U> |
flatMap(Function<? super T,? extends Optional<? extends U>> mapper) |
If a value is present, returns the result of applying the given
Optional-bearing mapping function to the value, otherwise returns
an empty Optional.
|
T |
get() |
If a value is present, returns the value, otherwise throws
NoSuchElementException.
|
int |
hashCode() |
Returns the hash code of the value, if present, otherwise 0
(zero) if no value is present.
|
void |
ifPresent(Consumer<? super T> action) |
If a value is present, performs the given action with the value,
otherwise does nothing.
|
void |
ifPresentOrElse(Consumer<? super T> action,
Runnable emptyAction) |
If a value is present, performs the given action with the value,
otherwise performs the given empty-based action.
|
boolean |
isPresent() |
If a value is present, returns true, otherwise false.
|
<U> Optional<U> |
map(Function<? super T,? extends U> mapper) |
If a value is present, returns an Optional describing (as if by
ofNullable(T)) the result of applying the given mapping function to
the value, otherwise returns an empty Optional.
|
static <T> Optional<T> |
of(T value) |
Returns an Optional describing the given non-null
value.
|
static <T> Optional<T> |
ofNullable(T value) |
Returns an Optional describing the given value, if
non-null, otherwise returns an empty Optional.
|
Optional<T> |
or(Supplier<? extends Optional<? extends T>> supplier) |
If a value is present, returns an Optional describing the value,
otherwise returns an Optional produced by the supplying function.
|
T |
orElse(T other) |
If a value is present, returns the value, otherwise returns
other.
|
T |
orElseGet(Supplier<? extends T> supplier) |
If a value is present, returns the value, otherwise returns the result
produced by the supplying function.
|
<X extends Throwable> T |
orElseThrow(Supplier<? extends X> exceptionSupplier) |
If a value is present, returns the value, otherwise throws an exception
produced by the exception supplying function.
|
Stream<T> |
stream() |
If a value is present, returns a sequential Stream containing
only that value, otherwise returns an empty Stream.
|
String |
toString() |
Returns a non-empty string representation of this Optional
suitable for debugging.
|
-
-
Method Detail
-
ofNullable
public static <T> Optional<T> ofNullable(T value)
Returns an Optional describing the given value, if
non-null, otherwise returns an empty Optional.
- Type Parameters:
T - the type of the value
- Parameters:
value - the possibly-null value to describe
- Returns:
- an
Optional with a present value if the specified value
is non-null, otherwise an empty Optional
-
isPresent
public boolean isPresent()
If a value is present, returns true, otherwise false.
- Returns:
true if a value is present, otherwise false
-
ifPresent
public void ifPresent(Consumer<? super T> action)
If a value is present, performs the given action with the value,
otherwise does nothing.
- Parameters:
action - the action to be performed, if a value is present
- Throws:
NullPointerException - if value is present and the given action is
null
-
ifPresentOrElse
public void ifPresentOrElse(Consumer<? super T> action,
Runnable emptyAction)
If a value is present, performs the given action with the value,
otherwise performs the given empty-based action.
- Parameters:
action - the action to be performed, if a value is present
emptyAction - the empty-based action to be performed, if no value is
present
- Throws:
NullPointerException - if a value is present and the given action
is null, or no value is present and the given empty-based
action is null.
- Since:
- 9
-
filter
public Optional<T> filter(Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate,
returns an Optional describing the value, otherwise returns an
empty Optional.
- Parameters:
predicate - the predicate to apply to a value, if present
- Returns:
- an
Optional describing the value of this
Optional, if a value is present and the value matches the
given predicate, otherwise an empty Optional
- Throws:
NullPointerException - if the predicate is null
-
or
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
If a value is present, returns an Optional describing the value,
otherwise returns an Optional produced by the supplying function.
- Parameters:
supplier - the supplying function that produces an Optional
to be returned
- Returns:
- returns an
Optional describing the value of this
Optional, if a value is present, otherwise an
Optional produced by the supplying function.
- Throws:
NullPointerException - if the supplying function is null or
produces a null result
- Since:
- 9
-
orElse
public T orElse(T other)
If a value is present, returns the value, otherwise returns
other.
- Parameters:
other - the value to be returned, if no value is present.
May be null.
- Returns:
- the value, if present, otherwise
other
-
orElseGet
public T orElseGet(Supplier<? extends T> supplier)
If a value is present, returns the value, otherwise returns the result
produced by the supplying function.
- Parameters:
supplier - the supplying function that produces a value to be returned
- Returns:
- the value, if present, otherwise the result produced by the
supplying function
- Throws:
NullPointerException - if no value is present and the supplying
function is null
-
orElseThrow
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
throws X extends Throwable
If a value is present, returns the value, otherwise throws an exception
produced by the exception supplying function.
- API Note:
- A method reference to the exception constructor with an empty argument
list can be used as the supplier. For example,
IllegalStateException::new
- Type Parameters:
X - Type of the exception to be thrown
- Parameters:
exceptionSupplier - the supplying function that produces an
exception to be thrown
- Returns:
- the value, if present
- Throws:
X - if no value is present
NullPointerException - if no value is present and the exception
supplying function is null
X extends Throwable
-
toString
public String toString()
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.
- Overrides:
toString in class Object
- Implementation Requirements:
- If a value is present the result must include its string representation
in the result. Empty and present
Optionals must be unambiguously
differentiable.
- Returns:
- the string representation of this instance
|