Java9 Optional Class Improvements
[ Java 9 New Features](#)
The Optional class was introduced in Java 8 to effectively address NullPointerExceptions. In Java 9, three methods were added to enhance its functionality:
* stream()
* ifPresentOrElse()
* or()
### stream() Method
**Syntax**
public Stream stream()
The purpose of the stream method is to convert 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>list = Arrays.asList(Optional.empty(), Optional.of("A"), Optional.empty(), Optional.of("B")); ListfilteredList = list.stream() .flatMap(o ->o.isPresent() ? Stream.of(o.get()) : Stream.empty()) .collect(Collectors.toList()); ListfilteredListJava9 = list.stream() .flatMap(Optional::stream) .collect(Collectors.toList()); System.out.println(filteredList); System.out.println(filteredListJava9); }}
The execution output is:
[A, B][A, B]
### ifPresentOrElse() Method
**Syntax**
public void ifPresentOrElse(Consumer action, Runnable emptyAction)
The improvement of the ifPresentOrElse method is the addition of an "else" clause. It accepts two parameters: a Consumer and a Runnable.
The purpose of the ifPresentOrElse method is: if an Optional contains a value, it calls the function `action` on that value, i.e., `action.accept(value)`, which is consistent with `ifPresent`. The difference from the `ifPresent` method is that `ifPresentOrElse` has a second parameter, `emptyAction` β if the Optional does not contain a value, then `ifPresentOrElse` will call `emptyAction`, i.e., `emptyAction.run()`.
## Example
import java.util.Optional; public class Tester{public static void main(String[]args){Optionaloptional = 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.")); }}
The execution output is:
Value: 1Not Present.
### or() Method
**Syntax**
public Optional or(Supplier<? extends Optional> supplier)
If a value exists, returns the Optional with the specified value; otherwise, returns a preset value.
## Example
import java.util.Optional; import java.util.function.Supplier; public class Tester{public static void main(String[]args){Optionaloptional1 = Optional.of("Mahesh"); Supplier<Optional>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)); }}
The execution output is:
Value: MaheshValue: Not Present
[ Java 9 New Features](#)
YouTip