Java 9 & 10: 4 new methods in the Optional API
One of the coolest things that where introduced in Java 8 was the Optional API.
In short terms, Optional is a container object that may or may not contain a non-null value. It also offers a great range of methods that you can use to interact with the value of the
Optional in a nice functional fashion.
With the release of Java 9 and Java 10 even more new methods were added to the Optional API.
In this post we're going to take a look at what they can do.
Let's get started!
Do something if present, and do something else if not present
If you've been using Optionals for a while, you may have used the
ifPresent allows us to do is to provide a consumer function that will be executed if the Optional contains a value.
To see how this is done, say we got an Optional containing an article —
Now we want to publish the article if it exists in the
Cool right? Well it gets better.
With the release of Java 9 a new variant of this was released called
What this one is doing is to giving us the chance to also provide an alternative function that will be executed if the optional is empty.
ifPresentOrElse out by not only providing a publish method, but also an alternative
Runnable function that will create a notification saying that the article wasn't published.
article.ifPresentOrElse( this::publish, this::notifyThatNothingWasPublished );
Provide a supplier that creates an alternative Optional
The next new method introduced in the Optional API is the
This method is enabling us to provide a supplier with an alternative Optional that will be returned if the original Optional is empty.
As an example, say we got a method —
getFeaturedArticle — that returns an Optional containing a featured article.
Now, there may be scenarios where we don't have a featured article, meaning the Optional will be empty. In these scenario we should use a backup article provided by the method
getFeatureArticle().or(() -> Optional.of(getBackupArticle()));
As you can see, we simply provide a Supplier that creates the backup Optional.
Converting an Optional to a Stream
Another new method introduced in the Optional API is the
What this method does is to convert our Optional into a Stream, meaning that if the Optional contains a value it'll become a Stream of one element. If the Optional is empty, it'll create an empty stream.
To see how this is done, say we want to join our Optional containing a featured article with a Stream containing the latest articles.
Stream.concat( getFeatureArticle().stream(), getLatestArticles() );
That's it! We simply turn our Optional into a Stream, then join it with the stream of the latest articles using
Return the value if present. If not, throw an Exception
The last new method we're going to look at is the one that was introduced in Java 10 called
This method returns the value of an Optional if it exists. If the Optional is empty however, it'll throw a
This is a quite similar method to the
Optional.orElseThrow(exceptionSupplier), which allows us to define the exception by wrapping it in a Supplier.
So that's it — 4 new methods introduced for Optional in Java 9 and Java 10.