Guide to Java Optional : Part 2

Mrinal Gupta
Nerd For Tech
Published in
3 min readFeb 27, 2024

In the last article, we saw what optional is? why do we need it? How to check if the data is present in optional? How to fetch that data? and what to do if data is not present and we want to have a default value in place of it? Now in this article, we are going to dive deeper into Java Optional and cover all those things we promised in article 1. Keep reading for more…

Throwing Exceptions

There might be some cases where we just want to throw the exceptions when the wrapped value is not present, Optional provides a a method orElseThrow(), using which we can throw the exceptions. It comes with two variations, the plain orElseThrow() and orElseThrow(exceptionSupplier). Let’s look at official docs.

As you can see above, the first one throws the NoSuchElementException with a message “No value present” and the second one takes a supplier to get the exception and then throws it.

without supplier
with supplier, throws exception produced by supplier (InterrupptedException)

map() in optional

Yes, you read it right, just like stream, we also have map() in optional class in java. It is similar to stream differing in one aspect that it takes a function (mapper or transformer) and returns an optional if the value is present or returns empty optional if value is absent. If the mapper function passed is null., it throws NullPointerException.

map() in optional

Let’s see the possible use.

map() in practice

Notice the code above, until findFirst() every operation was processing a stream but after it every operation is returning an optional, including map() method.

Similarly, we have filter method in optional. It applies a predicate if optional is present and true if the predicates evaluates or empty if predicate does not evaluate. If the optional is not present, it returns empty. If predicate provided is null, it throws NullPointerException similar to map().

This concludes almost all the methods present in optional class. Now time for confusions.

I am a puzzle master…

There is one more method ifPresent(). Wait! there is isPresent() as well. Wait! there is isEmpty() as well. What the hell? I will not try to give a complex explanation of above three, instead we will again refer to official docs.

difference between similar looking three methods.

isPresent() returns boolean based on optional is present or not. isEmpty() is just the opposite of isPresent() which returns true if optional is not present. On the other hand, ifPresent() takes a consumer and returns void, meaning it performs an action based on if optional is present else it does nothing.

Tada!!! confusion solved. Although I tried to include everything here in this article. But we still have to discuss serialisation and places where we should avoid using optionals.

That is something or later articles. If you want to read more of my blogs, please follow and give this one a clap if it helped you.

Happy coding!

--

--

Mrinal Gupta
Nerd For Tech

Software Developer on weekdays and A reading enthusiast on weekends. Beethoven’s silence and Chopin’s minute waltz intrigue me.