Java 8 Stream with batch processing

前端 未结 15 912
醉梦人生
醉梦人生 2020-11-28 02:42

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

15条回答
  •  星月不相逢
    2020-11-28 03:18

    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 classifier) {
        return collect(Collectors.groupingBy(classifier));
    }
    

    (Disclaimer: I work for the company behind jOOλ)

提交回复
热议问题