Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java 9 - Optional class improvements

0 contributions
2 points

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
0 contributions

Suggested wiki articles:

Suggested findings

Let's do the work and have some fun
  • Dirask is online IT community for professionals and hobbyist to share their knowledge and help each other in extraordinary easy way. ❤ 💻 🙂
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    we will help you. 👍 ✔ 🦄
Read more