I have a large file that contains a list of items.
I would like to create a batch of items, make an HTTP request with this batch (all of the items are needed as par
Note! This solution reads the whole file before running the forEach.
You could do it with jOOλ, a library that extends Java 8 streams for single-threaded, sequential stream use-cases:
Seq.seq(lazyFileStream) // Seq
.zipWithIndex() // Seq>
.groupBy(tuple -> tuple.v2 / 500) // Map>
.forEach((index, batch) -> {
process(batch);
});
Behind the scenes, zipWithIndex() is just:
static Seq> zipWithIndex(Stream stream) {
final Iterator it = stream.iterator();
class ZipWithIndex implements Iterator> {
long index;
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public Tuple2 next() {
return tuple(it.next(), index++);
}
}
return seq(new ZipWithIndex());
}
... whereas groupBy() is API convenience for:
default Map> groupBy(Function super T, ? extends K> classifier) {
return collect(Collectors.groupingBy(classifier));
}
(Disclaimer: I work for the company behind jOOλ)