Java 8 Optional Class
Optional class is a container object which can be null. If the value exists, the isPresent() method returns true, and calling the get() method returns the object.
Optional is a container: it can hold a value of type T, or just null. Optional provides many useful methods, so we don't have to explicitly perform null value checks.
The introduction of the Optional class effectively solves the null pointer exception.
Class declaration
The following is ajava.util.Optional<T>class declaration:
Class methods
| Serial number | Method & Description |
|---|---|
| 1 | static <T> Optional<T> empty()
Returns an empty Optional instance. |
| 2 | boolean equals(Object obj)
Determines whether another object is equal to the Optional. |
| 3 | Optional<T> filter(Predicate<? super <T> predicate)
If the value exists and matches the given predicate, returns an Optional describing this value; otherwise, returns an empty Optional. |
| 4 | <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)
If a value exists, returns the value based on the mapping method contained in the Optional; otherwise, returns an empty Optional. |
| 5 | T get()
If the value is contained in this Optional, returns the value; otherwise, throws an exception: NoSuchElementException |
| 6 | int hashCode()
Returns the hash code of the present value, or 0 if the value is not present. |
| 7 | void ifPresent(Consumer<? super T> consumer)
If a value is present, invokes the consumer with the value; otherwise, does nothing. |
| 8 | boolean isPresent()
The method returns true if the value is present, otherwise returns false. |
| 9 | <U>Optional<U> map(Function<? super T,? extends U> mapper)
If a value is present, applies the mapping function to it and gets the return value. If the return value is not null, creates an Optional containing the mapped return value as the return value of the map method; otherwise, returns an empty Optional. |
| 10 | static <T> Optional<T> of(T value)
Returns an Optional with the specified non-null value. |
| 11 | static <T> Optional<T> ofNullable(T value)
If the value is non-null, returns an Optional describing the specified value; otherwise, returns an empty Optional. |
| 12 | T orElse(T other)
If the value is present, returns the value; otherwise, returns other. |
| 13 | T orElseGet(Supplier<? extends T> other)
If the value is present, returns the value; otherwise, invokes other and returns the result of the other call. |
| 14 | <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
If the value is present, returns the contained value; otherwise, throws an exception provided by the Supplier. |
| 15 | String toString()
Returns a non-null string of the Optional, used for debugging. |
Note:These methods are inherited fromjava.lang.Objectthe class.
Optional Instance
We can better understand the usage of the Optional class through the following examples:
Java8Tester.java file
Executing the above script produces the following output:
$ javac Java8Tester.java $ java Java8Tester 第一个参数值存在: false 第二个参数值存在: true 10
Other Extensions
Java 8 New Features