以下代码将对象流拆分为 1000 个块,在物化时处理它们并在最后返回对象总数。
在所有情况下,返回的数字都是正确的,除非流大小恰好为 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();