After we’ve made our first big steps into functional programming in the last post, we will talk about Optionals in today’s part.
Why do we need Optionals?
Hand on heart, you also think that null is annoying, don’t you? For every argument which can be null, you have to check whether it is null or not:
In Java 8, java.util.Optional
How do optionals work?
Optional have two states. They either hold an object or they hold null. If they hold an object, Optional are called present, if they hold null, they are called empty. If they are not empty, you can get the object in the Optional by using Optional.get(). But be careful, because a get() on an empty optional will cause a NoSuchElementException. You can check if a optional is present by calling the method Optional.isPresent(). Below is a simple example:
1. Working with Optional and null
2. Using isPresent() and get()
2.1. Doing something when the value is present
2.2. Returning a Modified Version of the Value
There is another really cool method that you can use with Optionals. It’s called flatMap(). When you use map on a method that returns an Optional, you basically get an Optional
This is the reason there is flatMap. It flattens the Optional
You can find a lot of books, talks and discussions about the question: Should you use null or Optional in some particular case. And both have their right to be used. In the linked talk, you will find a nice rule which you can apply in most of the cases. Use Optionals when “there is a clear need to represent ‘no result’ or where null is likely to cause errors.”. So you shouldn’t use Optionals like this:
That’s it for today! We have played with the Optional class. It’s a container class for other classes which is either present or not present(empty). We have removed some common code smell that comes with Optionals and used functions as objects again. We also discussed when you should use null and when Optionals. In the next part, we will use Streams as a new way to handle a Collection of Objects.
* An Introduction to Functional Programming in Java ... Part 1 - Functions as Objects
* An Introduction to Functional Programming in Java 8: Part 3 - Streams