diff --git a/lib/abacus-util-0.9.52.jar b/lib/abacus-util-0.9.52.jar index a8b17b5c..f7eb9fea 100644 Binary files a/lib/abacus-util-0.9.52.jar and b/lib/abacus-util-0.9.52.jar differ diff --git a/lib/abacus-util-all-0.9.52.jar b/lib/abacus-util-all-0.9.52.jar index 16e7a201..f4830b67 100644 Binary files a/lib/abacus-util-all-0.9.52.jar and b/lib/abacus-util-all-0.9.52.jar differ diff --git a/src/com/landawn/abacus/util/stream/AbstractStream.java b/src/com/landawn/abacus/util/stream/AbstractStream.java index ae0bbb3f..400b5687 100644 --- a/src/com/landawn/abacus/util/stream/AbstractStream.java +++ b/src/com/landawn/abacus/util/stream/AbstractStream.java @@ -636,24 +636,30 @@ public Stream collapse(final BiPredicate collapsible, f return this.newStream(new ImmutableIterator() { private T pre = (T) NONE; - private T res = null; @Override public boolean hasNext() { - return iter.hasNext(); + return pre != NONE || iter.hasNext(); } @Override public T next() { - T next = iter.next(); + T res = pre == NONE ? (pre = iter.next()) : pre; + + boolean hasMore = false; - if (pre == NONE || collapsible.test(pre, next) == false) { - res = next; - } else { - res = mergeFunction.apply(res, next); + while (iter.hasNext()) { + if (collapsible.test(pre, (pre = iter.next()))) { + res = mergeFunction.apply(res, pre); + } else { + hasMore = true; + break; + } } - pre = next; + if (hasMore == false) { + pre = (T) NONE; + } return res; } @@ -666,24 +672,30 @@ public Stream collapse(final R seed, final BiPredicate() { private T pre = (T) NONE; - private R res = null; @Override public boolean hasNext() { - return iter.hasNext(); + return pre != NONE || iter.hasNext(); } @Override public R next() { - T next = iter.next(); + R res = mergeFunction.apply(seed, pre == NONE ? (pre = iter.next()) : pre); + + boolean hasMore = false; - if (pre == NONE || collapsible.test(pre, next) == false) { - res = mergeFunction.apply(seed, next); - } else { - res = mergeFunction.apply(res, next); + while (iter.hasNext()) { + if (collapsible.test(pre, (pre = iter.next()))) { + res = mergeFunction.apply(res, pre); + } else { + hasMore = true; + break; + } } - pre = next; + if (hasMore == false) { + pre = (T) NONE; + } return res; } @@ -697,24 +709,31 @@ public Stream collapse(final Supplier supplier, final BiPredicate() { private T pre = (T) NONE; - private C res = null; @Override public boolean hasNext() { - return iter.hasNext(); + return pre != NONE || iter.hasNext(); } @Override public C next() { - T next = iter.next(); + final C res = supplier.get(); + mergeFunction.accept(res, pre == NONE ? (pre = iter.next()) : pre); - if (pre == NONE || collapsible.test(pre, next) == false) { - res = supplier.get(); - } + boolean hasMore = false; - mergeFunction.accept(res, next); + while (iter.hasNext()) { + if (collapsible.test(pre, (pre = iter.next()))) { + mergeFunction.accept(res, pre); + } else { + hasMore = true; + break; + } + } - pre = next; + if (hasMore == false) { + pre = (T) NONE; + } return res; }