Optional
Class is a container object may be a null. If the value exists isPresent()
method returns true
, call the get()
method returns the object.
Optional
A container: it can hold the value of type T, or just to save null
. Optional
Offers many useful ways, so we do not explicitly be null detection.
Optional
The introduction of class a good solution to a null pointer exception.
① test
public class OptionalTest {
public static void main(String[] args) {
OptionalTest test = new OptionalTest();
Integer val1 = null;
Integer val2 = new Integer(10);
//Optional.ofNullable - 允许传递为 null 参数
Optional<Integer> op1 = Optional.ofNullable(val1);
//Optional.of - 如果传递的参数是 null,抛出异常 NullPointerException
Optional<Integer> op2 = Optional.of(val2);
System.out.println(test.sum(op1,op2));
}
public Integer sum(Optional<Integer> a, Optional<Integer> b) {
//Optional.isPresent():判断值是否为null
System.out.println("参数1的值是否为null:" + a.isPresent());
System.out.println("参数2的值是否为null:" + b.isPresent());
//Optional.orElse - 如果值存在,返回它,否则返回默认值
Integer val1 = a.orElse(0);
//Optional.get - 获取值,值需要存在
Integer val2 = b.get();
return val1+val2;
}
}
② class method
No. | Method and Description |
---|---|
1 | static <T> Optional<T> empty() Optional return empty instance. |
2 | boolean equals(Object obj) Determining whether another object is equal Optional. |
3 | Optional<T> filter(Predicate<? super <T> predicate) If the value is present, and this value matches the given predicate , returns a value to describe Optional otherwise an empty Optional. |
4 | <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) If the value is present, the return value mapping method comprising Optional based, otherwise an empty Optional |
5 | T get() If you include the value in this Optional, the return value, otherwise an exception is thrown: NoSuchElementException |
6 | int hashCode() Returns the hash code value exists, returns 0 if the value does not exist. |
7 | void ifPresent(Consumer<? super T> consumer) If the value exists then use that value to call consumer, or do nothing. |
8 | boolean isPresent() Returns true if the value of the method exists, otherwise false. |
9 | <U>Optional<U> map(Function<? super T,? extends U> mapper) If there is value, then calling the mapping function to get its return value. If the return value is not null , then create a map that contains the return value of Optional a map method returns a value, otherwise return empty Optional . |
10 | static <T> Optional<T> of(T value) Optional returns a non-null value specified. |
11 | static <T> Optional<T> ofNullable(T value) If non-empty, return Optional The described specified value, otherwise empty Optional. |
12 | T orElse(T other) If the value is present, the return value, otherwise other. |
13 | T orElseGet(Supplier<? extends T> other) If the value is present, return values, or trigger other, and returns the result other call. |
14 | <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) If the value is present, the value contained, otherwise an exception is thrown inherited by Supplier |
15 | String toString() Optional returns a non-null string, used to debug |