Java 8 Stream with batch processing

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

    With Java 8 and com.google.common.collect.Lists, you can do something like:

    public class BatchProcessingUtil {
        public static  List process(List data, int batchSize, Function, List> processFunction) {
            List> batches = Lists.partition(data, batchSize);
            return batches.stream()
                    .map(processFunction) // Send each batch to the process function
                    .flatMap(Collection::stream) // flat results to gather them in 1 stream
                    .collect(Collectors.toList());
        }
    }
    

    In here T is the type of the items in the input list and U the type of the items in the output list

    And You can use it like this:

    List userKeys = [... list of user keys]
    List users = BatchProcessingUtil.process(
        userKeys,
        10, // Batch Size
        partialKeys -> service.getUsers(partialKeys)
    );
    

提交回复
热议问题