Optional in collections

Sometime it is argued that the type Optional is worth being used in collections. It allegedly solves the problem of e.g.: HashMap that returns null in case there is no mapping for a key as well as when the value null is mapped to the key. If you use a Map<Optional<Something>> then you can clearly separate a missing mapping and an absent value. And that way you are one level deeper in the rabbit hole.

First of all

you can

tell if a key is mapped to null or not mapped without using Optional. There is the method containsKey(). This is one more method call to separate the non-mapped key from a mapped null value. However calling the methods of Optional is also. So what is the point? On the other hand

you do not need

to tell if the key is mapped to null or the mapping is missing. If there is a difference in your program code between the two cases then you created the code for the business login in a wrong way. This is certainly code smell. Think of null as “nothing”, and instead of thinking “null is assigned to the key ‘aaaaaarrghhh'” say out loud: Nothing is assigned to the key ‘aaaaaarrghhh’. You see? There is no difference except that all look at you now in the office.

And using optional as a value in a Map

you will

end up one level deeper in the rabbit hole after a while. Code lives independent life. It is not only you, who develop it. In large organizations there are developers who are certainly drunk when code. (This is the only reasonable explanation for some code.) They will soon populate your Map<Optional<Something>> with

  • null values,
  • absent Optional values
  • and even with Optional object that wrap something else but not your “Something“.

From time to time, if you are lucky you may even find some non null, non absent Optional<Something> values.

Advertisements

5 thoughts on “Optional in collections

  1. David Kovacs

    I agree in not using Optional. Using Optional instead of null has only one justification, it makes more visible in the code when something can be optional/null or not. The @NonNull and @Nullable annotation solves this cleaner.

    Like

    Reply
  2. Robert

    It seems to me that Optional is really designed around certain types of functional chaining. I think it tends to work (better) in languages like Scala, but I question how useful it really is in Java. To me at the moment I don’t see a good value. If Java were to move a lot further into being a functional language then maybe it would become more valuable; but with so much need for backwards compatibility I just don’t see that happening anytime soon (it ever).

    Like

    Reply
  3. Pingback: Use of optional is optional | Java Deep

  4. Pingback: Using Java 8's 'Optional' is Optional -

  5. Pingback: Using Java 8's 'Optional' is Optional | JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s