Java 8 Stream with batch processing

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

    I wrote a custom Spliterator for scenarios like this. It will fill lists of a given size from the input Stream. The advantage of this approach is that it will perform lazy processing, and it will work with other stream functions.

    public static  Stream> batches(Stream stream, int batchSize) {
        return batchSize <= 0
            ? Stream.of(stream.collect(Collectors.toList()))
            : StreamSupport.stream(new BatchSpliterator<>(stream.spliterator(), batchSize), stream.isParallel());
    }
    
    private static class BatchSpliterator implements Spliterator> {
    
        private final Spliterator base;
        private final int batchSize;
    
        public BatchSpliterator(Spliterator base, int batchSize) {
            this.base = base;
            this.batchSize = batchSize;
        }
    
        @Override
        public boolean tryAdvance(Consumer> action) {
            final List batch = new ArrayList<>(batchSize);
            for (int i=0; i < batchSize && base.tryAdvance(batch::add); i++)
                ;
            if (batch.isEmpty())
                return false;
            action.accept(batch);
            return true;
        }
    
        @Override
        public Spliterator> trySplit() {
            if (base.estimateSize() <= batchSize)
                return null;
            final Spliterator splitBase = this.base.trySplit();
            return splitBase == null ? null
                    : new BatchSpliterator<>(splitBase, batchSize);
        }
    
        @Override
        public long estimateSize() {
            final double baseSize = base.estimateSize();
            return baseSize == 0 ? 0
                    : (long) Math.ceil(baseSize / (double) batchSize);
        }
    
        @Override
        public int characteristics() {
            return base.characteristics();
        }
    
    }
    

提交回复
热议问题