I have the following block of code:
Optional<Integer> result = //some method that returns an Optional<Integer>;
if(result.isPresent()) {
return result.get();
} else {
return 0;
}
and my IntelliJ suggests that I replace it with a functional expression. I see that there is a method ifPresentOrElse()
inside Optional
but I can't possibly figure out how to use it in this particular case.
Any suggestions? Thanks!
Looks like orElse()
is what you'd want here. https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#orElse-T-
Optional<Integer> result = //some method that returns an Optional<Integer>;
return result.orElse(0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With