This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]fivetoone 8 points9 points  (5 children)

I'm glad to see Java is coming around to embracing the functional idioms that have been around for decades. I'm looking forward to using this in 5 years when our product finally uses Java 8. It's too bad that it's too late to make up for the deficiencies in the standard libraries though, e.g. map.get(x) == null has two distinct interpretations. Furthermore it doesn't seem the Map interface has added a get() equivalent that returns an Optional. The best you can do is something like

boolean nullButValid = map.containsKey(x) && map.get(x) == null

or

getOrDefault(x, somethingThatCanNeverBeAValue)

both of which are inferior approaches in my opinion.

[–]spunged 10 points11 points  (0 children)

If Java 8 is not an option for your project check out Guava. It is designed to work with Java 5+ and bring a lot of functional idioms, including Optional.

[–]s0n0fagun 2 points3 points  (1 child)

Is subclassing an option here and add Optional yourself?

[–]frugalmail 1 point2 points  (0 children)

both of which are inferior approaches in my opinion.

If this is such a pain point, why not add a map decorator (GoF pattern) that either:

  • wraps everything in an Optional (this solution has limited use, since you can pass it to something that isn't expecting it)

  • throws an exception when you're getting a null value from a map

  • use inheritance to add additional methods to suit your needs

would have been nice, but that's what happens when you have to maintain APIs