diff --git a/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/processing/CustomBatchIterator.java b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/processing/CustomBatchIterator.java index b5407b7283..bfc7ffae3b 100644 --- a/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/processing/CustomBatchIterator.java +++ b/core-java-modules/core-java-streams-4/src/main/java/com/baeldung/streams/processing/CustomBatchIterator.java @@ -14,20 +14,20 @@ public class CustomBatchIterator implements Iterator> { private List currentBatch; private final Iterator iterator; - public CustomBatchIterator(Iterator sourceIterator, int batchSize) { + private CustomBatchIterator(Iterator sourceIterator, int batchSize) { this.batchSize = batchSize; this.iterator = sourceIterator; } @Override public List next() { + prepareNextBatch(); return currentBatch; } @Override public boolean hasNext() { - prepareNextBatch(); - return currentBatch != null && !currentBatch.isEmpty(); + return iterator.hasNext(); } public static Stream> batchStreamOf(Stream stream, int batchSize) {