September 2019
Intermediate to advanced
816 pages
18h 47m
English
Relying on Optional in collections can be a design smell. Take another 30 minutes to re-evaluate the problem and discover better solutions.
The preceding statement is valid especially in the case of Map when the reason behind this decision sounds like this—so, Map returns null if there is no mapping for a key or if null is mapped to the key, so I cannot tell whether the key is not present or is a missing value. I will wrap the values via Optional.ofNullable() and done!
But what will we decide further if Map of Optional<Foo> is populated with null values, absent Optional values, or even Optional objects that contain something else, but not Foo? Haven't we just nested the initial problem into one more layer? ...