In Java 8, I have a variable containing an optional boolean type.
I want the action to be executed if the optional parameter is not empty, and the boolean contained in it is true.
I dream of something like ifPresentAndTrue
, here is a complete example:
import java.util.Optional;
public class X {
public static void main(String[] args) {
Optional<Boolean> spouseIsMale = Optional.of(true);
spouseIsMale.ifPresentAndTrue(b -> System.out.println("There is a male spouse."));
}
}
source
share