Skip to content

Instantly share code, notes, and snippets.

@Primetalk
Last active November 26, 2018 18:42
Show Gist options
  • Save Primetalk/b590c2b671d4a42653e25e3785bbcfac to your computer and use it in GitHub Desktop.
Save Primetalk/b590c2b671d4a42653e25e3785bbcfac to your computer and use it in GitHub Desktop.
Slice a stream into batches (Java)
import java.util.*;
import java.util.stream.Stream;
public final class StreamUtils {
public static <A> Stream<List<A>> sliced(final Stream<A> stm, final int sliceSize){
final ArrayList<A> lst = new ArrayList<>(sliceSize);
final int[] cnt = {0};
final Stream<ArrayList<A>> sliced = stm.flatMap(el -> {
if (cnt[0] == sliceSize) {
cnt[0] = 0;
ArrayList<A> batch = new ArrayList<>(lst);
lst.clear();
return Stream.of(batch);
} else {
cnt[0] += 1;
lst.add(el);
return Stream.empty();
}
});
return Stream.concat(sliced, Stream.generate(() -> lst));
}
}
@coacoas
Copy link

coacoas commented Nov 26, 2018

Why are you using an int[] rather than, say, an AtomicInteger?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment