[英]Short circuit logical operators over Iterable
考慮下面的代碼,循環一旦到達false
值就可以結束。 有沒有比每次迭代后檢查false
更好的方法了?
boolean result = true;
List<Boolean> bList = new ArrayList<>();
for (boolean b : bList) {
result = result && b;
if (!result) {
break;
}
}
關於什么
if (bList.contains(false)) {
...
考慮將循環提取到其方法:
boolean allTrue(List<Boolean> bools) {
for (boolean b : bools)
if (!b)
return false;
}
return true;
}
使用Stream.allMatch這是一種短路操作。
List<Boolean> bList = new ArrayList<>();
boolean result = bList.stream().allMatch(b -> b);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.