Trying to use Optional.ofNullalbe in java 8 to replace the below traditional if else code
String result = null;
if ( a!= null ) {
result = a.getVal1();
if(result == null) {
result = a.getVal2();
}
}
and ended up like below to achieve the result. Is there a better way of doing it.
result = Optional.ofNullable(a)
.map(A::getVal1)
.orElse(
Optional.ofNullable(a)
.map(A::getVal2)
.orElse(null)
);