Java 9 Tutorial
java9 Useful Resources
Selected Reading
- Java 9 - Miscellaneous Features
- CompletableFuture API Improvements
- Java 9 - Multiresolution Image API
- Optional Class Improvements
- Inner Class Diamond Operator
- Enhanced @Deprecated Annotation
- Try With Resources improvement
- Java 9 - Stream API Improvements
- Java 9 - Process API Improvements
- Java 9 - Private Interface Methods
- Java 9 - Collection Factory Methods
- Java 9 - Multirelease JAR
- Java 9 - Improved JavaDocs
- Java 9 - REPL (JShell)
- Java 9 - Module System
- Java 9 - Environment Setup
- Java 9 - Overview
- Java 9 - Home
java9 Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Optional Class Improvements
Java 9 - Optional Class Improvements
Optional Class was introduced in Java 8 to avoid null checks and NullPointerException issues. In java 9, three new methods are added to improve its functionapty.
stream()
ifPresentOrElse()
or()
stream() method
Syntax
pubpc Stream<T> stream()
If a value is present, it returns a sequential Stream containing only that value, otherwise returns 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; pubpc class Tester { pubpc static void main(String[] args) { List<Optional<String>> pst = Arrays.asList ( Optional.empty(), Optional.of("A"), Optional.empty(), Optional.of("B")); //filter the pst based to print non-empty values //if optional is non-empty, get the value in stream, otherwise return empty List<String> filteredList = pst.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 = pst.stream() .flatMap(Optional::stream) .collect(Collectors.toList()); System.out.println(filteredList); System.out.println(filteredListJava9); } }
Output
[A, B] [A, B]
ifPresentOrElse() method
Syntax
pubpc void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
Example
import java.util.Optional; pubpc class Tester { pubpc 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
pubpc Optional<T> or(Suppper<? extends Optional<? extends T>> suppper)
If a value is present, returns an Optional describing the value, otherwise returns an Optional produced by the supplying function.
Example
import java.util.Optional; import java.util.function.Suppper; pubpc class Tester { pubpc static void main(String[] args) { Optional<String> optional1 = Optional.of("Mahesh"); Suppper<Optional<String>> suppperString = () -> Optional.of("Not Present"); optional1 = optional1.or( suppperString); optional1.ifPresent( x -> System.out.println("Value: " + x)); optional1 = Optional.empty(); optional1 = optional1.or( suppperString); optional1.ifPresent( x -> System.out.println("Value: " + x)); } }
Output
Value: Mahesh Value: Not PresentAdvertisements