In Java 8, I have a variable, holding an optional boolean.
I want an action to be executed, if the optional is not empty, and the contained boolean is true.
I am dreaming about something like ifPresentAndTrue
, here a full 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."));
}
}
For good order
if (spouseIsMale.orElse(false)) {
System.out.println("There is a male spouse.");
}
Clear.