English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Optional class in Java 8 The introduction of the Optional class effectively solves the null pointer exception problem in Java. 9 Among them, three methods have been added to improve its functionality:
stream()
ifPresentOrElse()
or()
Syntax
public Stream<T> stream()
The stream method is used to convert 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()).
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); } }
The execution output result is:
[A, B] [A, B]
Syntax
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
The improvement of the ifPresentOrElse method is that it has else, accepting two parameters Consumer and Runnable.
The purpose of the ifPresentOrElse method is that if an Optional contains a value, it calls the function action that contains the 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, then ifPresentOrElse will call emptyAction, i.e., emptyAction.run().
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.")); } }
The execution output result is:
Value: 1 Not Present.
Syntax
public Optional<T> or(Supplier<? extends Optional<? extends T>> supplier)
If the value exists, return the value specified by Optional, otherwise return a predefined value.
import java.util.Optional; import java.util.function.Supplier; public class Tester { public static void main(String[] args) { Optional<String> optional1 = Optional.of("Sea"); 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)); } }
The execution output result is:
Value: Sea Value: Not Present