Java 8 Stream with batch processing -
i have large file contains list of items.
i create batch of items, make http request batch  (all of items needed parameters in http request). can for loop, java 8 lover, want try writing java 8's stream framework (and reap benefits of lazy processing).
example:
list<string> batch = new arraylist<>(batch_size); (int = 0; < data.size(); i++) {   batch.add(data.get(i));   if (batch.size() == batch_size) process(batch); }  if (batch.size() > 0) process(batch); i want long line of lazyfilestream.group(500).map(processbatch).collect(tolist())
what best way this?
you jooλ, library extends java 8 streams single-threaded, sequential stream use-cases:
seq.seq(lazyfilestream)              // seq<string>    .zipwithindex()                   // seq<tuple2<string, long>>    .groupby(tuple -> tuple.v2 / 500) // map<long, list<string>>    .foreach((index, batch) -> {        process(batch);    }); behind scenes, zipwithindex() just:
static <t> seq<tuple2<t, long>> zipwithindex(stream<t> stream) {     final iterator<t> = stream.iterator();      class zipwithindex implements iterator<tuple2<t, long>> {         long index;          @override         public boolean hasnext() {             return it.hasnext();         }          @override         public tuple2<t, long> next() {             return tuple(it.next(), index++);         }     }      return seq(new zipwithindex()); } ... whereas groupby() api convenience for:
default <k> map<k, list<t>> groupby(function<? super t, ? extends k> classifier) {     return collect(collectors.groupingby(classifier)); } (disclaimer: work company behind jooλ)
Comments
Post a Comment