简体   繁体   English

组合谓词练习 JAVA hyperskill

[英]Composing predicates excercise JAVA hyperskill

I am practicing my java skills on Hyperskill and I cant figure out this excercise about composing predicates.我正在 Hyperskill 上练习我的 java 技能,但我无法弄清楚这个关于编写谓词的练习。

Write the disjunctAll method that accepts a list of IntPredicate's and returns a single IntPredicate.编写接受 IntPredicate 列表并返回单个 IntPredicate 的 disjunctAll 方法。 The result predicate is a disjunction of all input predicates.结果谓词是所有输入谓词的析取。

If the input list is empty then the result predicate should return false for any integer value (always false).如果输入列表为空,则结果谓词应为任何 integer 值返回 false(始终为 false)。

Important.重要的。 Pay attention to the provided method template.注意提供的方法模板。 Do not change it.不要改变它。

public static IntPredicate disjunctAll(List<IntPredicate> predicates) {

}

A simple iteration of the list would do it:一个简单的列表迭代就可以做到:

    public static IntPredicate disjunctAll(List<IntPredicate> predicates)
    {
        IntPredicate result = i -> false;
        for (IntPredicate p: predicates) {
            result = p.or(result);
        }
        return result;
    }

or simply with a stream reducer:或简单地使用 stream 减速器:

    public static IntPredicate disjunctAll(List<IntPredicate> predicates)
    {
        return predicates.stream()
            .reduce(i -> false, IntPredicate::or);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM