[英]Composing predicates excercise JAVA hyperskill
我正在 Hyperskill 上練習我的 java 技能,但我無法弄清楚這個關於編寫謂詞的練習。
編寫接受 IntPredicate 列表並返回單個 IntPredicate 的 disjunctAll 方法。 結果謂詞是所有輸入謂詞的析取。
如果輸入列表為空,則結果謂詞應為任何 integer 值返回 false(始終為 false)。
重要的。 注意提供的方法模板。 不要改變它。
public static IntPredicate disjunctAll(List<IntPredicate> predicates) {
}
一個簡單的列表迭代就可以做到:
public static IntPredicate disjunctAll(List<IntPredicate> predicates)
{
IntPredicate result = i -> false;
for (IntPredicate p: predicates) {
result = p.or(result);
}
return result;
}
或簡單地使用 stream 減速器:
public static IntPredicate disjunctAll(List<IntPredicate> predicates)
{
return predicates.stream()
.reduce(i -> false, IntPredicate::or);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.