Java 8 - Optional Class

Every Java Programmer is familiar with NullPointerException. It can crash your code. And it is very hard to avoid it without using too many null checks. 

So, to overcome this, Java 8 has introduced a new class Optional in java.util package. It can help in writing a neat code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run. 

Example-

// Java program without Optional Class

public class OptionalDemo {

    public static void main(String[] args)
    {
        String[] words = new String[10];
        String word = words[5].toLowerCase();
        System.out.print(word);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException

Solution:-

To avoid abnormal termination, we use the Optional class. In the following example, we are using Optional. So, our program can execute without crashing.

The above program using Optional Class

// Java program with Optional Class

import java.util.Optional;

public class OptionalDemo {

    public static void main(String[] args)
    {
        String[] words = new String[10];
        Optional<String> checkNull = Optional.ofNullable(words[5]);

        if (checkNull.isPresent()) {
            String word = words[5].toLowerCase();
            System.out.print(word);
        }
        else
            System.out.println("word is null");
    }
}


Output:-

word is null


Optional is a container object which may or may not contain a non-null value. You must import java.util package to use this class. 

Optional  Class Method :-

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.


This class inherits methods from the following class −

java.lang.Object


Optional Example:-

Java8TOptionalClass.java

import java.util.Optional;

public class Java8TOptionalClass{

   public static void main(String args[]) {

      Java8TOptionalClass obj = new Java8TOptionalClass();
      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);
      System.out.println(obj.sum(a,b));
   }

   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;
   }
}

Output:-

First parameter is present: false

Second parameter is present: true

10