Easy Tutorial
❮ Java Arraylist Sublist Java Hashmap Containsvalue ❯

Java 8 Optional Class

Java 8 New Features


The Optional class is a container object which may or may not contain a null value. If a value is present, the isPresent() method will return true and the get() method will return the object.

Optional is a container: it can hold a value of type T or just hold null. Optional provides many useful methods so that we don't have to explicitly perform null checks.

The introduction of the Optional class effectively addresses the issue of null pointer exceptions.

Class Declaration

Here is the declaration for the java.util.Optional<T> class:

public final class Optional<T>
extends Object

Class Methods

No. Method & Description
1 static <T> Optional<T> empty() Returns an empty Optional instance.
2 boolean equals(Object obj) Indicates whether some other object is "equal to" this Optional.
3 Optional<T> filter(Predicate predicate) If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
4 <U> Optional<U> flatMap(Function mapper) If a value is present, return the result of applying the Optional-bearing mapping function to the value, otherwise return an empty Optional.
5 T get() If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
6 int hashCode() Returns the hash code value of the present value, or 0 if no value is present.
7 void ifPresent(Consumer consumer) If a value is present, invoke the specified consumer with the value, otherwise do nothing.
8 boolean isPresent() Returns true if there is a value present, otherwise false.
9 <U>Optional<U> map(Function 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. Otherwise, return an empty Optional.
10 static <T> Optional<T> of(T value) Returns an Optional with the specified present non-null value.
11 static <T> Optional<T> ofNullable(T value) Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
12 T orElse(T other) Returns the value if present, otherwise returns other.
13 T orElseGet(Supplier other) Returns the value if present, otherwise invokes other and returns the result of that invocation.
14 <X extends Throwable> T orElseThrow(Supplier exceptionSupplier) Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier.
15 String toString() Returns a non-null string representation of this Optional, suitable for debugging.

Note: These methods are inherited from java.lang.Object.


Optional Example

We can better understand the usage of the Optional class through the following example:

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 passed parameter to be null
Optional<Integer> a = Optional.ofNullable(value1);

// Optional.of - Throws NullPointerException if the passed argument is null
Optional<Integer> b = Optional.of(value2);
System.out.println(java8Tester.sum(a,b));
}

public Integer sum(Optional<Integer> a, Optional<Integer> b){

// Optional.isPresent - Checks if the value is present

System.out.println("First parameter is present: " + a.isPresent());
System.out.println("Second parameter is present: " + b.isPresent());

// Optional.orElse - Returns the value if present, otherwise returns the default value
Integer value1 = a.orElse(new Integer(0));

// Optional.get - Gets the value, the value should be present
Integer value2 = b.get();
return value1 + value2;
}
}

Executing the above script, the output is:

$ javac Java8Tester.java 
$ java Java8Tester
First parameter is present: false
Second parameter is present: true
10

Java 8 New Features

❮ Java Arraylist Sublist Java Hashmap Containsvalue ❯