Most visited

Recently visited

Added in API level 24

Optional

public final class Optional
extends Object

java.lang.Object
   ↳ java.util.Optional<T>


容器对象,其中可能包含或不包含非空值。 如果值存在, isPresent()将返回trueget()将返回值。

还提供了依赖于包含值是否存在的其他方法,例如 orElse() (如果值不存在,则返回默认值)和 ifPresent() (如果值存在, ifPresent()执行一个代码块)。

Summary

Public methods

static <T> Optional<T> empty()

返回一个空的 Optional实例。

boolean equals(Object obj)

指示某个其他对象是否“等于”this可选。

Optional<T> filter(Predicate<? super T> predicate)

如果存在值,并且该值与给定谓词匹配,则返回描述该值的 Optional ,否则返回空 Optional

<U> Optional<U> flatMap(Function<? super T, Optional<U>> mapper)

如果存在值,则应用提供的 Optional映射函数,返回该结果,否则返回空 Optional

T get()

如果此值存在于此 Optional ,则返回该值,否则将引发 NoSuchElementException

int hashCode()

返回当前值的哈希码值(如果有的话),如果没有值,则返回0(零)。

void ifPresent(Consumer<? super T> consumer)

如果存在值,则使用该值调用指定的消费者,否则不执行任何操作。

boolean isPresent()

如果存在值,则返回 true ,否则 false

<U> Optional<U> map(Function<? super T, ? extends U> mapper)

如果存在一个值,则应用提供的映射函数,如果结果不为空,则返回描述结果的 Optional

static <T> Optional<T> of(T value)

用指定的当前非空值返回 Optional

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

返回描述指定值的 Optional ,如果非null,则返回空 Optional

T orElse(T other)

返回值如果存在,否则返回 other

T orElseGet(Supplier<? extends T> other)

返回值如果存在,否则调用 other并返回该调用的结果。

<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)

返回包含的值(如果存在),否则抛出由提供的供应商创建的异常。

String toString()

返回适用于调试的此可选的非空字符串表示形式。

Inherited methods

From class java.lang.Object

Public methods

empty

Added in API level 24
Optional<T> empty ()

返回一个空的Optional实例。 此可选项不存在任何值。

API Note:
  • Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by Option.empty(). There is no guarantee that it is a singleton. Instead, use isPresent().
Returns
Optional<T> an empty Optional

equals

Added in API level 24
boolean equals (Object obj)

指示某个其他对象是否“等于”this可选。 另一个对象被认为是相等的,如果:

  • 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 Object: an object to be tested for equality
Returns
boolean {code true} if the other object is "equal to" this object otherwise false

filter

Added in API level 24
Optional<T> filter (Predicate<? super T> predicate)

如果存在一个值,并且该值与给定的谓词匹配,则返回描述该值的 Optional ,否则返回一个空的 Optional

Parameters
predicate Predicate: a predicate to apply to the value, if present
Returns
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
Throws
NullPointerException if the predicate is null

flatMap

Added in API level 24
Optional<U> flatMap (Function<? super T, Optional<U>> mapper)

如果存在值,则应用提供的Optional映射函数,返回该结果,否则返回空Optional 此方法类似于map(Function) ,但所提供的映射器的结果已经是Optional ,如果调用,则flatMap不会将其封装为额外的Optional

Parameters
mapper Function: a mapping function to apply to the value, if present the mapping function
Returns
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
Throws
NullPointerException if the mapping function is null or returns a null result

get

Added in API level 24
T get ()

如果此值存在于此 Optional ,则返回该值,否则将抛出 NoSuchElementException

Returns
T the non-null value held by this Optional
Throws
NoSuchElementException if there is no value present

也可以看看:

hashCode

Added in API level 24
int hashCode ()

返回当前值的哈希码值(如果有的话),如果没有值,则返回0(零)。

Returns
int hash code value of the present value or 0 if no value is present

ifPresent

Added in API level 24
void ifPresent (Consumer<? super T> consumer)

如果存在值,则使用该值调用指定的消费者,否则不执行任何操作。

Parameters
consumer Consumer: block to be executed if a value is present
Throws
NullPointerException if value is present and consumer is null

isPresent

Added in API level 24
boolean isPresent ()

如果存在值,则返回 true ,否则 false

Returns
boolean true if there is a value present, otherwise false

map

Added in API level 24
Optional<U> map (Function<? super T, ? extends U> mapper)

如果存在值,则将所提供的映射函数应用于该函数,如果结果不为空,则返回描述结果的Optional 否则返回一个空的Optional

API Note:
  • This method supports post-processing on optional values, without the need to explicitly check for a return status. For example, the following code traverses a stream of file names, selects one that has not yet been processed, and then opens that file, returning an Optional<FileInputStream>:
    Optional<FileInputStream> fis =
             names.stream().filter(name -> !isProcessedYet(name))
                           .findFirst()
                           .map(name -> new FileInputStream(name));
     
    Here, findFirst returns an Optional<String>, and then map returns an Optional<FileInputStream> for the desired file if one exists.
Parameters
mapper Function: a mapping function to apply to the value, if present
Returns
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
Throws
NullPointerException if the mapping function is null

of

Added in API level 24
Optional<T> of (T value)

用指定的当前非空值返回 Optional

Parameters
value T: the value to be present, which must be non-null
Returns
Optional<T> an Optional with the value present
Throws
NullPointerException if value is null

ofNullable

Added in API level 24
Optional<T> ofNullable (T value)

返回描述指定值的 Optional ,如果非null,则返回空 Optional

Parameters
value T: the possibly-null value to describe
Returns
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
T orElse (T other)

返回值如果存在,否则返回 other

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

orElseGet

Added in API level 24
T orElseGet (Supplier<? extends T> other)

返回值如果存在,否则调用 other并返回该调用的结果。

Parameters
other Supplier: a Supplier whose result is returned if no value is present
Returns
T the value if present otherwise the result of other.get()
Throws
NullPointerException if value is not present and other is null

orElseThrow

Added in API level 24
T orElseThrow (Supplier<? extends X> exceptionSupplier)

返回包含的值(如果存在),否则抛出由提供的供应商创建的异常。

API Note:
  • A method reference to the exception constructor with an empty argument list can be used as the supplier. For example, IllegalStateException::new
Parameters
exceptionSupplier Supplier: The supplier which will return the exception to be thrown
Returns
T the present value
Throws
if there is no value present
NullPointerException if no value is present and exceptionSupplier is null
Throwable

toString

Added in API level 24
String toString ()

返回适用于调试的此可选的非空字符串表示形式。 确切的表示格式是未指定的,并且在实现和版本之间可能有所不同。

实现要求:
  • If a value is present the result must include its string representation in the result. Empty and present Optionals must be unambiguously differentiable.
Returns
String the string representation of this instance

Hooray!