I want to use java optional operator to avoid nullchecks like:
if(outher != null && outher.getNested() != null &&
outher.getNested().getInner() != null && outher.getNested().getAnotherInner() != null)
I looked through some tutorials in the Internet + Java cheatsheet, but did not find how to resolve the part outher.getNested.getInner() != null && outher.getNested.getAnotherInner != null.
So how to do it right:
Optional.of(outher)
.map(Outher::getNested)
.map(Nested::getInner)
.map(Nested::getAnotherInner)
.isPresent();
will be awesome
source
share