logo

Optional Class Improvements


Show

This optional class was launched in Java 8 to steer clear of null checks and NullPointersException Issues. Three new methods are added in Java 9 to enhance its functionality.

  • stream()
  • ifPresentOrElse()
  • or()

Streak Method()

Syntax

public Stream<T> stream()

If a value is available, it goes back on a sequential Stream having only that value, otherwise goes back on an empty Stream.

Example

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Tester {
public static void main(String[] args) {
   List<Optional<String>> list = Arrays.asList (
      Optional.empty(), 
      Optional.of("A"), 
      Optional.empty(), 
      Optional.of("B"));

      //filter the list based to print non-empty values

      //if optional is non-empty, get the value in stream, otherwise return empty
      List<String> filteredList = list.stream()
         .flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
         .collect(Collectors.toList());

      //Optional::stream method will return a stream of either one 
      //or zero element if data is present or not.

      List<String> filteredListJava9 = list.stream()
         .flatMap(Optional::stream)
         .collect(Collectors.toList());

      System.out.println(filteredList);
      System.out.println(filteredListJava9);
   }  
}

Output

[A, B]
[A, B]

ifPresentOrElse() Method

Syntax

public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)

If a value is available, perform the given task with the value, and if not then perform the given empty-based task.

Example

import java.util.Optional;

public class Tester {
   public static void main(String[] args) {
      Optional<Integer> optional = Optional.of(1);

      optional.ifPresentOrElse( x -> System.out.println("Value: " + x),() -> 
         System.out.println("Not Present."));


      optional = Optional.empty();

      optional.ifPresentOrElse( x -> System.out.println("Value: " + x),() -> 

         System.out.println("Not Present."));
   }  
}

Output

Value: 1
Not Present.

or() Method

Syntax

public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)

If a value is available, goes back on an Optional defining the value, and if not then goes back on an Optional generated by the supplying function.

Example

import java.util.Optional;
import java.util.function.Supplier;

public class Tester {
   public static void main(String[] args) {
      Optional<String> optional1 = Optional.of("Mahesh");

      Supplier<Optional<String>> supplierString = () -> Optional.of("Not Present");

      optional1 = optional1.or( supplierString);

      optional1.ifPresent( x -> System.out.println("Value: " + x));
    
      optional1 = Optional.empty();    

      optional1 = optional1.or( supplierString);
  
      optional1.ifPresent( x -> System.out.println("Value: " + x));  
   }  
}

Output

Value: Mahesh
Value: Not Present