How to do an action if an optional boolean is true?
It is possible to achieve that behaviour with .filter(b -> b)
:
spouseIsMale.filter(b -> b).ifPresent(b -> System.out.println("There is a male spouse."));
However, it costs some brain execution time seconds to understand what is going on here.
For good order
if (spouseIsMale.orElse(false)) {
System.out.println("There is a male spouse.");
}
Clear.