VOOZH about

URL: https://www.javacodegeeks.com/2018/07/optional-isempty-available-jdk-11.html

⇱ Optional.isEmpty() Available in JDK 11 EA Builds - Java Code Geeks


My recently posted question “Optional.isEmpty() Coming to Java?” was prompted by a core-libs-dev mailing list post titled “RFR: 8184693: (opt) add Optional.isEmpty“. The current JDK 11 Early Access builds (such as OpenJDK JDK Early Access Build 23 that I use in this post) now include the isEmpty() method on the “Optional” classes Optional, OptionalDouble, OptionalInt, and OptionalLong. This allows for more fluent expression in cases that formerly relied upon negation of Optional.isPresent() [or !OptionalDouble.isPresent(), !OptionalInt.isPresent(), or !OptionalLong.ifPresent()] as was done previously.

The next simple and contrived code listing demonstrates Optional.isEmpty().

public static void demonstrateOptionalIsEmpty()
{
 final Optional<String> middleName = getMiddleName();
 if (middleName.isEmpty())
 {
 out.println("There is no middle name!");
 }
}

Although the same functionality that Optional.isEmpty() provides can be achieved with !Optional.isPresent(), there are advantages to having these types of “isEmpty” methods available in the APIs of commonly used collection and data-holding classes. The ! symbol is more easily missed when reading and reviewing code than is an explicitly named method such as “isEmpty().” Having such a method also aligns Optional‘s API for detecting “empty” more closely to that provided by String [String.isEmpty()],Collection [Collection.isEmpty()], and Map [Map.isEmpty()].

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: Optional.isEmpty() Available in JDK 11 EA Builds

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Dustin Marx
Dustin Marx
July 23rd, 2018Last Updated: July 23rd, 2018
0 94 1 minute read
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz