Languages
[Edit]
EN

Java 9 - Optional class improvements

2 points
Created by:
Kate_C
20380

1. Overview

In java 9 there are 3 main improvements of Optional class:

  1. Optional.ifPresentOrElse();
  2. Optional.or();
  3. Optional.stream();

Below we have code example and explanation how all those 3 new Optional improvements work in java 9.

2. Optional.ifPresentOrElse

Example when optional is present

// 1st example when optional is present
Optional<String> optional = Optional.of("Test");
AtomicInteger successCounter = new AtomicInteger();
AtomicInteger emptyOptionalsCounter = new AtomicInteger();

// if optional is present then execute first lambda expression
// if optional is empty then execute second lambda expression
optional.ifPresentOrElse(x -> successCounter.incrementAndGet(),
        () -> emptyOptionalsCounter.incrementAndGet());

// optional was present so we incremented successCounter
System.out.println(successCounter.get()); // 1
System.out.println(emptyOptionalsCounter.get()); // 0

Example - when optional is empty

Optional<String> optional = Optional.empty();
AtomicInteger successCounter = new AtomicInteger();
AtomicInteger emptyOptionalsCounter = new AtomicInteger();

optional.ifPresentOrElse(x -> successCounter.incrementAndGet(),
        () -> emptyOptionalsCounter.incrementAndGet());

// optional was empty so we incremented emptyOptionalCounter
System.out.println(successCounter.get()); // 0
System.out.println(emptyOptionalsCounter.get()); // 1

3. Optional.or

Example when optional1 is present

Optional<String> optional1 = Optional.of("Test1");
Optional<String> optional2 = Optional.of("Test2");

// if optional1 is present then return optional1
// if optional1 is empty then return optional2
Optional<String> optional3 = optional1.or(() -> optional2);
System.out.println(optional3.get()); // Test1

Example when optional1 is empty

Optional<String> optional1 = Optional.empty();
Optional<String> optional2 = Optional.of("Test2");

Optional<String> optional3 = optional1.or(() -> optional2);
System.out.println(optional3.get()); // Test2

4. Optional.stream

With Optional.stream we don't need to add additional logic to check if optional is present or not.

Example when optional is present

Optional<String> optional = Optional.of("Test");
long count = optional.stream().count();
System.out.println(count); // 1

Example when optional is empty

Optional<String> optional = Optional.empty();
long count = optional.stream().count();
System.out.println(count); // 0

5. Optional.get - changes proposal

There is proposal submitted by Stuart Marks to deprecate

Optional.get();

and rename it to something else.

Details of the proposal can be read here:
RFR(m): 8140281 deprecate Optional.get()

NOTE
compiled with jdk 9.0.1

References

  1. Java 9 - Optional JavaDoc
  2. RFR(m): 8140281 deprecate Optional.get()
  3. Java 9 - wikipedia
Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - friendly IT community for everyone.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join