在 Java 8 中,我有一个变量,包含一个可选的布尔值。
如果可选项不为空,并且包含的布尔值为真,我想要执行一个动作。
我梦想着类似 ifPresentAndTrue
的东西,这里有一个完整的例子:
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."));
}
}
原文由 slartidan 发布,翻译遵循 CC BY-SA 4.0 许可协议
为了良好的秩序
清除。