gpt4 book ai didi

java - 如果仅对 BinaryOperator 参数中的一个进行总结,Java 流实际上会减少什么?

转载 作者:搜寻专家 更新时间:2023-11-01 01:31:10 24 4
gpt4 key购买 nike

看看下面的代码:在二元运算符中,我们有 reduce((x,y)->x+x)。为什么实际计算到Optional[512]呢?我没有解释。

System.out.println((Stream.generate(()->1d).limit(10).
peek((doubleValue)->{
System.out.println("Call the first peek: "+doubleValue);
}).
reduce((x,y)->x+x)));

这是输出:为了向您说明,我在 peek 部分显示了单个 x 为 1.0。

Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Call the first peek: 1.0
Optional[512.0]

那么问题是,在获得 Optional[512] 之前,是什么控制 reduce 工作?

最佳答案

因为你有 10 个参数,但操作是 9。2^9 = 512

关于java - 如果仅对 BinaryOperator 参数中的一个进行总结,Java 流实际上会减少什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095965/

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