Home » Java » Core Java » Optional.isEmpty() Coming to Java?

About Dustin Marx

Dustin Marx

Optional.isEmpty() Coming to Java?

JDK-8184693 requests that the method isEmpty() be added to the Optional class introduced with JDK 8. Invoking Optional.isEmpty() would be the equivalent of invoking !Optional.isPresent(). There is no JDK release currently associated with JDK-8184693, but it is being actively worked as demonstrated in a recent core-libs-dev mailing list post titled “RFR: 8184693: (opt) add Optional.isEmpty“.

Written by Stuart Marks in July 2017, JDK-8184693 provides some interesting justification for the addition of Optional.isEmpty(). Marks points out that “generally we avoid adding methods that are simple inverses of each other” and cites as examples presence of String.isEmpty() and Collection.isEmpty() without any accompanying String.notEmpty() or Collection.nonEmpty() counterparts. Marks writes this approach works well in these cases because “emptiness/non-emptiness isn’t fundamental” for them: “For these objects, it’s perfectly reasonable to operate on an empty String (e.g., searching or appending it) or collection (e.g., iterating over it).”

In JDK-8184693, Marks writes of examples that do have methods to explicitly express both emptiness and non-emptiness. He writes, “However, with references, null/non-null is pretty fundamental, we have Objects.isNull and Objects.nonNull.” Because these examples’ usages are more like Optional‘s usages, Marks argues that Optional should have an isEmpty() method alongside its current isPresent() method: “Similarly with Optional, the empty/present dichotomy is quite fundamental, so there should be isEmpty alongside of isPresent.”

Most of the justification text in JDK-8184693 was added this month (April 2018) and includes a link to the April 2017 core-lib-devs mailing list post “Optional.isEmpty()” by Peter Levart. The bug write-up summarizes some of the discussion started by this post. Messages in that thread include those that provide humor, reference bikeshedding, list “plenty of one-liners that don’t use boolean negation,” recommend name isNotPresent() or isAbsent(), provide enthusiastic support of the idea of Optional.isEmpty(), and remind that “the bar for adding methods to Optional is set very high.”

The previously mentioned mailing list message “RFR: 8184693: (opt) add Optional.isEmpty” references code available for review. The “Sdiff” of Optional.java for this proposed change shows that this method has been implemented. However, a similar change still needs to be made for OptionalDouble, OptionalLong, and OptionalInt.

As I’ve used Optional in my Java code, I’ve come to appreciate times when I don’t need to use Optional.isPresent(). However, there are times when there’s no good way around it and I look forward to the addition of Optional.isEmpty() to replace the use of !Optional.isPresent(). The addition of Optional.isEmpty() is a minor thing, but I believe it will make my code more readable and more fluent. I look forward to it coming soon to a JDK near me.

Published on Java Code Geeks with permission by Dustin Marx, partner at our JCG program. See the original article here: Optional.isEmpty() Coming to Java?

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
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

Leave a Reply

avatar

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

  Subscribe  
Notify of