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.
I've earlier written about how Optional
can clean up your code.
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
method.
What 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 — Optional<Article>
.
Now we want to publish the article if it exists in the Optional
.
article.ifPresent(this::publish);
Cool right? Well it gets better.
With the release of Java 9 a new variant of this was released called ifPresentOrElse
.
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.
Let's try 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 or
.
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 getBackupArticle
.
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 stream
method.
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 Stream.concat
.
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 orElseThrow
.
This method returns the value of an Optional if it exists. If the Optional is empty however, it'll throw a NoSuchElementException
.
getFeatureArticle().orElseThrow();
This is a quite similar method to the Optional.orElseThrow(exceptionSupplier)
, which allows us to define the exception by wrapping it in a Supplier.
getFeatureArticle().orElseThrow(IllegalStateException::new);
More info
So that's it — 4 new methods introduced for Optional in Java 9 and Java 10.
If you want more info, checkout the post on Removing null checks with Optional or the official documentation.