Optional fromNullable()

Seibt, Volker volker.seibt at buenting.de
Fri Apr 26 00:30:03 PDT 2013

In contrast to the Guava API Optional class in Java contains no static method "fromNullable()" which allows null-values and  analogous should deliver Optional.empty() in this case.

It seems to be very comfortable in later "Optionalizing" return values like e.g.

Optional<T> o = Optional.fromNullable(map.get(key));

instead of

Optional<T> o = map.get(key) == null ? Optional.empty() :  Optional.of(map(key));

or worse readable

T t = map.get(key);
Optional<T> o = t == null ? Optional.empty() :  Optional.of(t);

to avoid calling map(..) twice.

Is there a reason why it's omitted?

More information about the lambda-dev mailing list