Java 9 Improved Optional Class

Java 9 新特性Java 9 New Features

The Optional class was introduced in Java 8. The introduction of the Optional class effectively solves the null pointer exception. In Java 9, three methods were added to improve its functionality:

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

stream() method

Syntax

public Stream<T> stream()

The stream method converts an Optional into a Stream. If the Optional contains a value, it returns a Stream containing that value; otherwise, it returns an empty Stream (Stream.empty()).

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

Executing the output result is:

[A, B]
[A, B]

ifPresentOrElse() method

Syntax

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

The improvement of the ifPresentOrElse method is that it has an else, accepting two parameters: Consumer and Runnable.

The purpose of the ifPresentOrElse method is: if an Optional contains a value, the function action is called on the contained value, i.e., action.accept(value), which is consistent with ifPresent; the difference from the ifPresent method is that ifPresentOrElse also has a second parameter, emptyAction — if the Optional does not contain a value, ifPresentOrElse calls emptyAction, i.e., emptyAction.run().

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.")); } }

Executing the output result is:

Value: 1
Not Present.

or() method

Syntax

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

If a value exists, return the value specified by Optional; otherwise, return a preset value.

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

Executing the output result is:

Value: Mahesh
Value: Not Present

Java 9 新特性Java 9 New Features