Java 8 Stream with batch processing

前端 未结 15 917
醉梦人生
醉梦人生 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:31

    Pure Java 8 example that works with parallel streams as well.

    How to use:

    Stream integerStream = IntStream.range(0, 45).parallel().boxed();
    CsStreamUtil.processInBatch(integerStream, 10, batch -> System.out.println("Batch: " + batch));
    

    The method declaration and implementation:

    public static  void processInBatch(Stream stream, int batchSize, Consumer> batchProcessor)
    {
        List newBatch = new ArrayList<>(batchSize);
    
        stream.forEach(element -> {
            List fullBatch;
    
            synchronized (newBatch)
            {
                if (newBatch.size() < batchSize)
                {
                    newBatch.add(element);
                    return;
                }
                else
                {
                    fullBatch = new ArrayList<>(newBatch);
                    newBatch.clear();
                    newBatch.add(element);
                }
            }
    
            batchProcessor.accept(fullBatch);
        });
    
        if (newBatch.size() > 0)
            batchProcessor.accept(new ArrayList<>(newBatch));
    }
    

提交回复
热议问题