I have to choose between two String variables - the first that has non null
value.
If they are both null
- then I want to exit the method.
This can be done in the following piece of code:
String value1 = <get from somewhere>
String value2 = <get from somewhere else>
String target = null;
if (value1 != null) target = value1;
else if (value2 != null) target = value2;
if (target == null) return null;
It can also be done in short form:
String target = value1 != null ? value1 : value2 != null ? value2 : null;
if (target == null) return null;
I am struggling with how to do this in fluent form with Optional
Note: I can only use Java 8 syntax (so no Optional.or()
)