gpt4 book ai didi

java - StreamEx 分组为列表返回不正确的记录数

转载 作者:行者123 更新时间:2023-11-30 06:33:18 26 4
gpt4 key购买 nike

以下代码将对象流拆分为 1000 个 block ,在具体化时对其进行处理,并最终返回对象总数。

在所有情况下,返回的数字都是正确的,除非流大小恰好为 1。在流大小为 1 的情况下,返回的数字为 0。

任何帮助将不胜感激。在流中没有记录为 0 的情况下,我还必须破解返回调用。我也想解决这个问题。

AtomicInteger recordCounter = new AtomicInteger(0);
try (StreamEx<MyObject> stream = StreamEx.of(myObjects)) {
stream.groupRuns((prev, next) -> recordCounter.incrementAndGet() % 1000 != 0)
.forEach((chunk) ->
{
//... process each chunk
}
);
} catch(Exception e) {
throw new MyRuntimeException("Failure streaming...", e);
} finally {
myObjects.close();
}

return recordCounter.get() == 0 ? 0 : recordCounter.incrementAndGet();

最佳答案

JavaDoc说:

sameGroup - a non-interfering, stateless predicate to apply to the pair of adjacent elements which returns true for elements which belong to the same group.

谓词必须是无状态的,但这不是您的情况。您滥用了该方法,这就是为什么您无法获得预期结果的原因。它的工作方式接近您想要的纯粹是偶然的,您不能依赖此行为,它可能会在未来的 StreamEx 版本中发生变化。

关于java - StreamEx 分组为列表返回不正确的记录数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45649990/

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