Java8减少了一个流

Ami*_*ani 1 java lambda java-8 java-stream

我有以下方法:

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);
}
Run Code Online (Sandbox Code Playgroud)

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

Sot*_*lis 5

您的编译问题是由于IntBinaryOperator#applyAsInt(int, int)需要两个参数.你只是声明/提供一个.

正如评论中所述,在查看javadoc之后IntStream#reduce(int, IntBinaryOperator),您实际上并没有应用有效的减少.我没有立即清楚你的意思,并在我定义的某个函数上汇总一个数字,Brian有一些建议.