Optional Class


Optional may be a container object wont to contain not-null objects. The optional object is employed to represent null with an absent value. This class has numerous utility methods to ease code to handle values as ‘available’ or ‘not available’ rather than checking null values. It's introduced in Java 8 and is analogous to what Optional is in Guava.

Class Declaration

Below are the Class Declaration for java.util.Optional<T> class −

public final class Optional<T> extends Object

Class Method


Method & Description


static <T> Optional<T> empty()

Returns an empty Non- Mandatory occurrence


boolean equals(Object obj)

Indicates whether some other object is "equal to" this unforced


Optional<T> filter(Predicate<? super <T> predicate)

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


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

If the worth is present, it applies the provided Optional-bearing mapping function thereto, returns that result, otherwise returns an empty Optional.


T get()

If a value is shown in this Optional, returns the value, if throws No Such Element Exception.


int hashCode()

Returns the hash code utility of the attending value, if any, or 0 (zero) if no value is available.


void ifPresent(Consumer<? super T> consumer)

If a value is attending, it entreats the defined consumer with the value, otherwise does nothing.


boolean isPresent()

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


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

If a value is available, then apply the provided mapping function to it, and if the outcome is non-null, return an Optional describing the result.


static <T> Optional<T> of(T value)

Returns a voluntary with the specified adjacent non-null value.


static <T> Optional<T> ofNullable(T value)

Returns an elective describing the specified value, if non-null, otherwise returns an empty free.


T orElse(T other)

Returns the value if present, if not returns the other.


T orElseGet(Supplier<? extends T> other)

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


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

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


String toString()

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

This Class come into the methods from the following Class-

  • java.lang.Object

Optional Example

Create the Java Program given in any editor of your own choice, in, say, C:\> JAVA.


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 passed parameter is null
      Optional<Integer> b = Optional.of(value2);
   public Integer sum(Optional<Integer> a, Optional<Integer> b) {
      //Optional.isPresent - checks the value is present or not
      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 passed.
      Integer value1 = a.orElse(new Integer(0));
      //Optional.get - gets the value, value should be present
      Integer value2 = b.get();
      return value1 + value2;

Check Your Result as given:

Assemble the whole class by using the javac compiler as given here:

C:\JAVA>javac Java8Tester.java

Now, run the Java8Tester as given:

C:\JAVA>java Java8Tester
You Outcome Should be:
First parameter is present: false
Second parameter is present: true