我试图将一组转换为Optional.OfNullable方法内的字符串,例如:
test.setAbc(Optional.ofNullable(rule.getSampleSet().toString()).orElse(null));
but if sampleSet
is null
it will give me a NullPointerException
.
can anyone tell me how to resolve this issue using .map
method with Optional
?
我知道一种通过预先检查可空性的传统方式:
if(rule.getSampeSet != null)
但是我很想知道我们能否一站式完成。
您是否正在寻找:
但老实说,我将使用if语句检查是否为null。
Instead of calling
toString()
inside theofNullable
, you couldmap
the optional to it: