Java 8 Optional Class

The Optional class is a nullable container object. The isPresent() method returns true if the value exists, and calling the get() method returns the object.
Optional is a container: it can hold the value of type T, or just save null. Optional provides a lot of useful methods so that we do not have to explicitly check for nulls.
The introduction of the Optional class solves null pointer exceptions very well.

Class declaration

The following is a declaration of a java.util.Optional<T> class:
public final class Optional<T> extends Object

Class method

No.Method & Description
1static <T> Optional<T> empty()
Returns an empty Optional instance.
2boolean equals(Object obj)
Determine if other objects are equal to Optional.
3Optional<T> filter(Predicate<? super <T> predicate)
If the value exists, and this value matches the given predicate, an Optional is returned to describe the value, otherwise an empty Optional is returned.
4<U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)
Returns the value based on the mapping method contained by Optional if the value exists, otherwise returns an empty Optional
5T get()
If you include this value in this Optional, return the value, otherwise throw an exception: NoSuchElementException
6int hashCode()
Returns the hash code of the existing value, or 0 if the value does not exist.
7void ifPresent(Consumer<? super T> consumer)
Use this value to call consumer if the value exists, otherwise nothing is done.
8boolean isPresent()
The method returns true if the value exists, false otherwise.
9<U>Optional<U> map(Function<? super T,? extends U> mapper)
If this value exists, the provided mapping method, if it returns non-null, returns an Optional description result.
10static <T> Optional<T> of(T value)
Returns an Optional that specifies a non-null value.
11static <T> Optional<T> ofNullable(T value)
If non-null, returns the specified value specified by Optional, otherwise returns an empty Optional.
12T orElse(T other)
If there is a value, the return value, otherwise it returns the other.
13T orElseGet(Supplier<? extends T> other)
If there is a value, the return value, otherwise it triggers other, and returns the result of the other call.
14<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)
If this value exists, return the contained value, otherwise throw the exception inherited by Supplier
15String toString()
Returns an Optional non-empty string for debugging
Note: These methods inherit from the java.lang.Object class.

Optional instance

We can use the following example to better understand the use of the Optional class:

Java8Tester.java file

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 - Allows passing null parameter Optional<Integer> a = Optional.ofNullable(value1); // Optional.of - If the passed argument is null, throws an 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 - determines if the value exists System.out.println("the first parameter value exists: " + a.isPresent()); System.out.println("the second parameter values exist: " + b.isPresent()); // Optional The. orElse - Returns the value if it exists, otherwise returns the default value Integer value1 = a.orElse(new Integer(0)); //Optional.get - get value, value needs to exist Integer value2 = b.get(); return value1 + value2; } }
Execute the above script, the output is:
$ javac Java8Tester.java 
$ java Java8Tester 
 the first parameter value exists: false
the second parameter values exist: true
10

Comments