Streamex - дополнение к Java Stream
Нашел простой способ разбивать поток элементов на группы с помощью StreamEx:
Stream<Integer> stream = IntStream.iterate(0, i -> i + 1).boxed().limit(15);
AtomicInteger counter = new AtomicInteger(0);
int chunkSize = 4;
StreamEx.of(stream)
.groupRuns((prev, next) -> counter.incrementAndGet() % chunkSize != 0)
.forEach(chunk -> System.out.println(chunk));
Выводит:
[0, 1, 2, 3]
[4, 5, 6, 7]
[8, 9, 10, 11]
[12, 13, 14]
Взял отсюда.