我有以下方法:
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的除数,并将我定义的函数(在本例中,是这个数字的平方)上的一个数字集合起来。
发布于 2014-05-30 23:16:00
您的编译问题是由于IntBinaryOperator#applyAsInt(int, int)采用两个参数的事实。你只是宣布/提供了一个。
正如注释中所述,在查看了IntStream#reduce(int, IntBinaryOperator)的javadoc之后,您实际上并没有应用有效的缩减。对于我来说,对于我定义的某个函数(但Brian has some suggestions )来说,您的意思还不太清楚,并将其集合为一个数字。
https://stackoverflow.com/questions/23964639
复制相似问题