English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Optional class is a container object that can be null. If the value exists, the isPresent() method will return true, and calling the get() method will return the object.
Optional is a container: it can save the value of type T, or simply save null. Optional provides many useful methods, so we don't have to explicitly perform null value detection.
The introduction of the Optional class is a good solution to the null pointer exception.
The following is an java.util.Optional<T> Class Declaration:
public final class Optional<T> extends Object
Serial Number | Method & Description |
---|---|
1 | static <T> Optional<T> empty() Returns an empty Optional example. |
2 | boolean equals(Object obj) Determine whether the other object is equal to Optional. |
3 | Optional<T> filter(Predicate<? super <T> predicate) If the value exists and matches the given predicate, return an Optional to describe this value; otherwise, return an empty Optional. |
4 | <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper) If the value exists, it will return an Optional based on the value of the mapping method contained in Optional; otherwise, it will return an empty Optional. |
5 | T get() If this value is contained in this Optional, return the value; otherwise, throw an exception: NoSuchElementException |
6 | int hashCode() Returns the hash code of the existing value, or 0 if the value does not exist. |
7 | void ifPresent(Consumer<? super T> consumer) If the value exists, it will use this value to call the consumer; otherwise, it will do nothing. |
8 | boolean isPresent() If the value exists, the method will return true; otherwise, it will return false. |
9 | <U>Optional<U> map(Function<? super T,? extends U> mapper) If the value exists, it will execute the mapping function to get the return value. If the return value is not null, it will create an Optional containing the mapping return value as the return value of the map method, otherwise it will return an empty Optional. |
10 | static <T> Optional<T> of(T value) Returns an Optional with a specified non-null value. |
11 | static <T> Optional<T> ofNullable(T value) If non-null, return the specified value described by Optional, otherwise return an empty Optional. |
12 | T orElse(T other) If the value exists, return the value, otherwise return other. |
13 | T orElseGet(Supplier<? extends T> other) If the value exists, return the value, otherwise trigger other and return the result of other's call. |
14 | <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) If the value exists, return the contained value, otherwise throw the exception inherited from Supplier |
15 | String toString() Returns a non-empty string in Optional for debugging |
Note: These methods are from java.lang.Object Inherited from the class.
We can better understand the use of the Optional class through the following examples:
import java.util.Optional; public class Java8Tester { public static void main(String args[]) { Java8Tester java8Tester = new Java8Tester(); Integer value1 = null; Integer value2 = new Integer(10; // Optional.ofNullable - Allow passing null parameters Optional<Integer> a = Optional.ofNullable(value1; // Optional.of - If the passed parameter is null, throw the exception NullPointerException Optional<Integer> b = Optional.of(value2; System.out.println(java8Tester.sum(a, b); } public Integer sum(Optional<Integer> a, Optional<Integer> b) { // Optional.isPresent - Determine if the value exists System.out.println("The first parameter value exists: "); + a.isPresent()); System.out.println("The second parameter value exists: "); + b.isPresent()); // Optional.orElse - If the value exists, return it, otherwise return the default value Integer value1 = a.orElse(new Integer(0)); //Optional.get - Get the value, the value must exist Integer value2 = b.get(); return value1 + value2; } }
Execute the above script, the output will be:
$ javac Java8Tester.java $ java Java8Tester The first parameter value exists: false The second parameter value exists: true 10