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
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)
);