I am looking to avoid multiple if-else
conditions. Is there a more concise way of writing the below code?
private Set<String> getValues(Optional<String> one, Optional<String> two) {
if (one.isPresent() && two.isPresent()) {
return ImmutableSet.of(one.get(), two.get());
} else if (one.isPresent()) {
return ImmutableSet.of(one.get());
} else {
return two.isPresent() ? ImmutableSet.of(two.get()) : ImmutableSet.of();
}
}
The simplest solution would be to use Optional.stream(), jdk9+:
You can read more here
If You are using JDK8 still:
And one extreme version, when You can pass any number of arguments