Java8减少流

阿米尔·阿富汗尼(Amir Afghani)

我有以下方法:

static IntStream streamedDivisors(final int n) {
    return IntStream.range(2, n).parallel().filter(input -> n % input == 0);
}

static int streamedPhi(final int n) {
    return streamedDivisors(n).reduce(0, x -> x * x);
}

并且在streamedPhi中遇到编译错误,表明我的lambda表达式中有不兼容的参数类型。有人可以帮助我理解这一点吗?我本质上是在尝试获取给定数字n的除数,并在我定义的某些函数上聚合一个数字(在这种情况下,对数字进行平方)。

Sotirios Delimanolis

您的编译问题是由于IntBinaryOperator#applyAsInt(int, int)有两个参数。您只声明/提供了一个。

如评论中所述,在查看的javadoc之后IntStream#reduce(int, IntBinaryOperator),您实际上并未应用有效的减少量。我现在还不清楚您的意思,并在我定义的某些函数上汇总一个数字,但是Brian有一些建议

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章