【发布时间】:2017-03-22 17:14:38
【问题描述】:
我需要根据另一个可选值获取一个可选值。这是我的意思的一个过于简化的例子:
Optional<SomeA> someA = callSomeFunctionThatReturnsOptionalSomeA(....);
Optional<SomeB> someB = // if someA is present, call some method to get value of someB, if someA is not present, don't bother and just return an empty optional.
我知道我可以使用 else/if for someA.isPresent() 来做到这一点,但如果可能的话,我想尝试使用更短的方式来做到这一点(使用可选的 map?)。有小费吗?
【问题讨论】:
-
三元组。或者,条件运算符
? :。