Last active
November 26, 2018 18:42
-
-
Save Primetalk/b590c2b671d4a42653e25e3785bbcfac to your computer and use it in GitHub Desktop.
Slice a stream into batches (Java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why are you using an
int[]
rather than, say, anAtomicInteger
?