以下代码将对象流拆分为1000块,在实例化过程中处理它们,并返回最后的对象总数。StreamEx分组到列表返回不正确的记录数
在号码返回的所有情况下是正确的,除非该流的大小恰好是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();
你为什么要返回'计数器+ 1'而不是'counter'? – wargre
因为否则它总是返回1比它应该少。 –