Posted by & filed under java.

Hope everybody already know that Java 8 is out. It brings a lot of interesting features to make Java easier and more comfortable to use. I will try to talk through some of those features in a nutshell.

Lets start with Optional class. As with all Java 8 features, Optional existed before the release, for example in Guava libraries. Optional class allows you to handle null checks in really nice way. Let’s look at the code. Say we have a User which can have a cat and lets say we have a map of two users Alice and Bob and Alice has a cat:

Now image all you have is a map and you want to get person’s cat name:

You can just lookup key in the map, get User object and get it’s cat and here you go. Unfortunately, couple of calls in this line can return null:

  1. Map.get() can return null
  2. User.getCat() can return null

To overcome that you will have to write two nested if statements as shown on lines 10-16. Quite a lot of code for such simple tasks, right? Imagine if Cat would also have it’s favourite food as well, which can have a brand and you need to get it. Then you will be doomed to add another if and spend end of your life trying to understand how to format in sensible way 4 closing brackets.

Fear not fellow Java developer! Optionals to the rescue! Lets create helper method which will get person’s cat name in NPE-safe way:

Now, let me explain what is happening here:

On line 2 we are wrapping result of map.get() function to Optional object. Optional class is class used to potentially hold some object. It has 3 methods which we touch here indirectly:

  • isPresent() — returns true if Optional is not empty
  • get() — returns object which Optional object holds or throws NoSuchElementException if Optional is empty
  • map() — function which accepts a function which transforms Optional<T> to another Optional<Y>. The magic here is that map will call function only if  original Optional will hold some value, otherwise it will just return empty optional, effectively doing Null check. Try looking at this code how map can work to convert Optional<String> to Optional<T>:

Now, lets get back to getCatName function. Once we got first Optional<User> we apply map function using lambda expression. I will cover them in another blog post, for now you need to understand that it is basically tells to call User::getCat() method BUT on User instance object if Optional<User> is not empty. Line  3 will return Optional<Cat> and we use same trick again to get Cat name, which is returning result as Optional<String>.

Now, we can demonstrate how to use this method:

As you can see, final code is much more clear than original if madness and it deliver same functionality and no NPE.

The “trick” here is that we never return null, we always return Optional object which holds information about previous operation results and allows itself to be used in subsequent operations.

I hope this blog post helped you understand hew Optional class in Java 8. If you have any questions, please use comment form below.

One Response to “Java 8 Features in a nutshell – Optional”

Leave a Reply

  • (will not be published)