public Stream<C> batchWhile(Predicate<? super T> predicate) { Iterator<T> it = stream.iterator(); return StreamUtils.stream( new Iterator<C>() { T value = (T) UNSET; @Override public boolean hasNext() { return value != UNSET || it.hasNext(); } @Override public C next() { C list = factory.get(); if (value != UNSET) list.add(value); T value; label: while (it.hasNext()) { value = it.next(); list.add(value); if (!predicate.test(value)) { value = (T) UNSET; break label; } value = (T) UNSET; } return list; } }) .filter(l -> l.size() > 0); }