创建: 2021/10/4
想想当年遇到新的,都会系统看一遍,把扁平的文字总结成一整张表格一样的博客。这一年很少能静下来这么做了,年纪也增长了不少,那种单纯的总结技术的时代似乎渐行渐远了。
不管怎样吧,从今天开始,以后也要静下心老老实实总结。
references:
https://www.baeldung.com/java-optional
https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
assign |
static Optional<T> empty()
|
Returns an empty Optional instance. |
|
Returns an Optional with the specified present non-null value. |
static Optional<T> ofNullable()
|
Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional . |
|
Value Presense |
isPresent |
|
This method returns true if the wrapped value is not null. |
void ifPresent(Consumer<? super T> consumer)
|
|
|
isEmpty
>= Java 11
|
|
|
Default Value |
|
|
T orElseGet(Supplier<? extends T> other)
|
|
<X extends Throwable>T orElseThrow(Suppiler<? extend X> exceptionSupplier)
|
|
|
get |
|
If a value is present in this Optional , returns the value, otherwise throws NoSuchElementException . |
|
filter |
Optional<T> filter(Predicate<? super T> predicate)
|
If a value is present, and the value matches the given predicate,
return an Optional describing the value, otherwise return an empty Optional .
|
|
map and flatMap |
<U> Optional<U> map(Function(? super T, ? 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.
|
<U> Optional<U> flatMap(Function(? super T, ? extends Optional<U>) mapper)
|
If a value is present, apply the provided Optional -bearing mapping function to it, return that result, otherwise return an empty Optional .
|
|
|
|
Chaining Options
in Java 8
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|