组合谓词练习 JAVA hyperskill

huangapple go评论59阅读模式
英文:

Composing predicates excercise JAVA hyperskill

问题

我正在Hyperskill上练习我的Java技能但是我无法弄清楚关于组合谓词的这个练习

编写disjunctAll方法接受一个IntPredicate的列表并返回一个单独的IntPredicate结果谓词是所有输入谓词的分离

如果输入列表为空则结果谓词对于任何整数值都应返回false始终为false)。

重要提示请注意所提供的方法模板不要对其进行更改

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

}
英文:

I am practicing my java skills on Hyperskill and I cant figure out this excercise about composing predicates.

Write the disjunctAll method that accepts a list of IntPredicate's and returns a single IntPredicate. 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).

Important. Pay attention to the provided method template. Do not change it.

public static IntPredicate disjunctAll(List&lt;IntPredicate&gt; predicates) {

}

答案1

得分: 3

一个简单的列表迭代就可以完成:

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

或者可以使用流操作的缩减器来简化:

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

A simple iteration of the list would do it:

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

or simply with a stream reducer:

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

huangapple
  • 本文由 发表于 2020年5月4日 17:10:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61588713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定