【问题标题】:IntPredicate Lambda Java8IntPredicate Lambda Java8
【发布时间】:2017-06-08 05:45:59
【问题描述】:

以下方法的功能是尝试通过添加两个数字的倍数来遍历从 3 到 999 的列表,其中multiplosDe.get(0) 可能是 4,multiplosDe.get(1) 可能是 7。

如果我只发送一个包含两个 Integer 对象的列表,则此功能有效。我想要的是能够发送 n 个对象的列表,而不必将 || multiplosDe.get(2) 添加到谓词。

public long sumaDeMultiplos(List<Integer> multiplosDe) {
    int suma = 0;
    IntPredicate predicate  = s -> (s%multiplosDe.get(0)==0 || s%multiplosDe.get(1)==0);

        suma = IntStream.range(3, 1000).filter(predicate).sum();

    return suma;
}

【问题讨论】:

    标签: lambda filter java-8 predicate


    【解决方案1】:
    IntPredicate predicate  = s -> multiplosDe.stream().anyMatch(i -> s % i == 0);
    

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 2021-08-13
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      相关资源
      最近更新 更多