Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - Optional Class



Optional class was introduced in Java 8 to simplify null pointer exception handling in code. A null pointer exception can come in cases where a method or property of a null object is invoked in code. Considering a very high possibility of a null pointer exception in code, developers generally try to handle null for each and every case but still being a runtime exception, programs remain error prone and chances of missing a null check remains high.

Optional instance is a container object used to contain not-null object/value. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as 'available' or 'not available' instead of checking null values. It is introduced in Java 8 and is similar to what Optional is in Guava.

Optional class provides a type check solution instead of directly checking the null value. This class acts as a wrapper over the value. Apart from handling null value, Optional class provides lots of utility methods like getting a default value in case of null value, throwing exception in case underlying value is null.

Optional Class Declaration

Following is the declaration for java.util.Optional<T> class −

public final class Optional<T> extends Object

Optional Class Methods

Sr.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<? super <T> predicate)

If a value is present and the value matches a given predicate, it returns an Optional describing the value, otherwise returns an empty Optional.

4

<U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)

If a value is present, it applies the provided Optional-bearing mapping function to it, returns that result, otherwise returns 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, if any, or 0 (zero) if no value is present.

7

void ifPresent(Consumer<? super T> consumer)

If a value is present, it invokes the specified consumer with the value, otherwise does nothing.

8

boolean isPresent()

Returns true if there is a value present, otherwise false.

9

<U>Optional<U> map(Function<? super T,? extends U> mapper)

If a value is present, applies the provided mapping function to it, and if the result is non-null, returns an Optional describing the result.

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<? extends T> other)

Returns the value if present, otherwise invokes other and returns the result of that invocation.

14

<X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier)

Returns the contained value, if present, otherwise throws an exception to be created by the provided supplier.

15

String toString()

Returns a non-empty string representation of this Optional suitable for debugging.

16

boolean isEmpty()

Returns true if there is a value present, otherwise false.

This class inherits methods from the following class −

  • java.lang.Object

Creating Optional Class Instance

Optional class provides multiple methods to create an Optional instance for a provided value/object. Following are the corresponding methods with their syntaxes followed by an example:

Optional.empty() method

This method returns an Optional instance with empty value. It can be used to represent an absent value instead of null.

Optional emptyOptional = Optional.empty();

Optional.of() method

This method returns an Optional instance with given not null value. If provided value is null, then it will throw a NullPointerException.

String name = "John";
Optional valueOptional = Optional.of(name);

Optional.ofNullable() method

This method returns an Optional instance with given value. If provided value is null, then it returns an empty Optional Instance.

Optional emptyOptional = Optional.empty();

Example: Creating Optional Instances

Following example showcases the use of above methods to create Optional objects and the cases when we should use them.

package com.tutorialspoint;

import java.util.Optional;

public class OptionalTester {
   public static void main(String[] args) {
      Integer value1 = null;
      Integer value2 = Integer.valueOf(10);

      // Optional.empty - return an empty optional object
      Optional<Integer> empty = Optional.empty();

      //Optional.ofNullable - allows passed parameter to be null.
      Optional<Integer> a = Optional.ofNullable(value1);

      //Optional.of - throws NullPointerException if passed parameter is null
      Optional<Integer> b = Optional.of(value2);

      System.out.println("value of a: " + (a.isPresent() ? a.get(): "0"));
      System.out.println("value of b: " +  (b.isPresent() ? b.get(): "0"));
      System.out.println("value of empty: " +  (empty.isPresent() ? empty.get(): "0"));
   }
}

Let us compile and run the above program, this will produce the following result −

value of a: 0
value of b: 10
value of empty: 0

Checking Optional Class Instance Value

Optional class provides following method to check if Optional instance has value or not. These methods should be used before using get() method to obtain the value of the Optional instance as get() method can throw null pointer exception in case underlying value is null.

Optional.isPresent() method

This method checks the current optional instance and returns true/false based on value being present or not.

Optional emptyOptional = Optional.empty();
boolean isValuePresent = emptyOptional.isPresent();

Optional.isEmpty() method

This method checks the current optional instance and returns true/false based on value being present or not. This method was added to Optional API in Java 11.

Optional emptyOptional = Optional.empty();
boolean isValuePresent = emptyOptional.isEmpty();

Example: Checking Optional Instances

Following example showcases the use of above methods to create Optional objects and the cases when we should use them.

package com.tutorialspoint;

import java.util.Optional;

public class OptionalTester {
   public static void main(String[] args) {
      Integer value1 = null;
      Integer value2 = Integer.valueOf(10);

      // Optional.empty - return an empty optional object
      Optional<Integer> empty = Optional.empty();

      //Optional.ofNullable - allows passed parameter to be null.
      Optional<Integer> a = Optional.ofNullable(value1);

      //Optional.of - throws NullPointerException if passed parameter is null
      Optional<Integer> b = Optional.of(value2);

      System.out.println("value of a: " + (a.isEmpty() ? a.get(): "0"));
      System.out.println("value of b: " +  (b.isPresent() ? b.get(): "0"));
      System.out.println("value of empty: " +  (empty.isPresent() ? empty.get(): "0"));
   }
}

Let us compile and run the above program, this will produce the following result −

value of a: 0
value of b: 10
value of empty: 0

Using Default Value With Optional Class Methods

Optional class provides following methods to get a default value from an Optional instance if value is not present.

Optional.OrElse() method

This method returns checks the current optional instance and returns its value if present otherwise it returns the provided default value.

Optional<Integer> valueOptional = Optional.ofNullable(null);
Integer value = valueOptional.orElse(Integer.valueOf(-1));

Optional.OrElseGet(Supplier) method

This method returns checks the current optional instance and returns its value if present otherwise it invokes the supplier function passed to the generate the default value and return the same.

Optional<Integer> valueOptional = Optional.ofNullable(null);
Integer value = valueOptional.orElseGet(()-> (int)(Math.random() * 10));

Example: Getting Default Values Using Optional Class Methods

Following example showcases the use of above methods to get default value using Optional objects.

package com.tutorialspoint;

import java.util.Optional;

public class OptionalTester {
   public static void main(String[] args) {
      // case 1: Optional is having null as underlying value
      Optional<Integer> valueOptional = Optional.ofNullable(null);
      // orElse will return -1 being default value
      Integer value = valueOptional.orElse(Integer.valueOf(-1));

      System.out.println(value);

      // case 2:  Optional is having not null as underlying value
      Optional<Integer> valueOptional1 = Optional.ofNullable(Integer.valueOf(10));

      //  orElse will return the underlying value
      Integer value1 = valueOptional1.orElse(Integer.valueOf(-1));

      System.out.println(value1);

      // case 3: Optional is having null as underlying value
      Optional<Integer> valueOptional2 = Optional.ofNullable(null);
      // orElse will return a random number between 0 to 10 for default value
      Integer value2 = valueOptional2.orElseGet(()-> (int)(Math.random() * 10));

      System.out.println(value2);

      // case 4:  Optional is having not null as underlying value
      Optional<Integer> valueOptional3 = Optional.ofNullable(Integer.valueOf(10));

      //  orElse will return the underlying value
      Integer value3 = valueOptional3.orElseGet(()-> (int)(Math.random() * 10));

      System.out.println(value3);	
   }	
}

Let us compile and run the above program, this will produce the following result −

-1
10
3
10

Optional Class Methods for Throwing Exception

Optional class provides following methods to throw exception in case value is not present.

Optional.OrElseThrow() method

We can throw a custom exception in case a required field is not passed to handle the case gracefully using orElseThrow() method call.

Optional<Integer> emptyOptional = Optional.empty();
// this call will throw NullPointerException
Integer value = emptyOptional.OrElseThrow();

Optional.OrElseThrow(Supplier) method

We can throw a custom exception in case a required field is not passed to handle the case gracefully using orElseThrow(supplier) method call.

Optional<Integer> emptyOptional = Optional.empty();
// this call will throw a custom exception as specified by the supplier function
Integer value = emptyOptional.orElseThrow(()-> {throw new RuntimeException("value not present");} );

Example: Throwing Exception Using Optional Class

Following example showcases the use of above methods to throw and handle exception in case of required paramters are missing.

package com.tutorialspoint;

import java.util.Optional;

public class OptionalTester {
   public static void main(String[] args) {
      Optional<Integer> valueOptional1 = Optional.ofNullable(null);
      Optional<Integer> valueOptional2 = Optional.ofNullable(10);

      try {
         // first value being null, NoSuchElementException will occur
         sum(valueOptional1, valueOptional2);
      }catch(Exception e){
         e.printStackTrace();
      }

      try {
         // second value being null, RuntimeException will occur
         sum(valueOptional2, valueOptional1);
      }catch(Exception e){
         e.printStackTrace();
      }
   }

   public static Integer sum(Optional<Integer> value1, Optional<Integer> value2) {
      // throws NoSuchElementException in case underlying value is not present
      Integer val1 = value1.orElseThrow();
      // throws a custom Exception as specified in case underlying value is not present
      Integer val2 = value2.orElseThrow(()-> {throw new RuntimeException("value not present");} );

      return val1 + val2;
   }	
}

Let us compile and run the above program, this will produce the following result −

java.util.NoSuchElementException: No value present
	at java.base/java.util.Optional.orElseThrow(Optional.java:377)
	at com.tutorialspoint.OptionalTester.sum(OptionalTester.java:27)
	at com.tutorialspoint.OptionalTester.main(OptionalTester.java:12)
java.lang.RuntimeException: value not present
	at com.tutorialspoint.OptionalTester.lambda$0(OptionalTester.java:29)
	at java.base/java.util.Optional.orElseThrow(Optional.java:403)
	at com.tutorialspoint.OptionalTester.sum(OptionalTester.java:29)
	at com.tutorialspoint.OptionalTester.main(OptionalTester.java:19)
Advertisements