gpt4 book ai didi

Java8减少流

转载 作者:行者123 更新时间:2023-11-29 07:47:52 26 4
gpt4 key购买 nike

我有以下方法:

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 的除数,并在我定义的某个函数上聚合一个数字(在这种情况下,对数字进行平方)。

最佳答案

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

如评论中所述,并在查看了 IntStream#reduce(int, IntBinaryOperator) 的 javadoc 之后,您实际上并没有应用有效的减少。我不是很清楚你所说的并在我定义的某个函数上聚合一个数字是什么意思,但是Brian has some suggestions .

关于Java8减少流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964639/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com