Process Java Collection in batches

The following snippets shows how to process a given Java Collection (List / Set) in batches and process each batch in sequence or parallel.

Split a given collection into batches

int batchSize = 2;
List<String> input = List.of(
"apple",
"banana",
"orange",
"mango",
"peach"
);
List<List<String>> batches = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(index -> index / batchSize))
.values()
.stream()
.map(indices -> indices.stream().map(input::get).toList())
.toList();

batches.forEach(values -> System.out.println("values = " + values));

Parallel process collection in batches using Java's ForkJoin.commonPool()

int batchSize = 2;
List<String> input = List.of(
"apple",
"banana",
"orange",
"mango",
"peach"
);
List<List<String>> batches = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(index -> index / batchSize))
.values()
.stream()
.map(indices -> indices.stream().map(input::get).toList())
.toList();

batches
.stream()
.parallel()
.forEach(values -> System.out.println("values = " + values));

Parallel process collection in batches using Java's Virtual Threads

int batchSize = 2;
List<String> input = List.of(
"apple",
"banana",
"orange",
"mango",
"peach"
);
List<List<String>> batches = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(index -> index / batchSize))
.values()
.stream()
.map(indices -> indices.stream().map(input::get).toList())
.toList();

try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
List<CompletableFuture<Void>> futures = batches.stream()
.map(values -> CompletableFuture
.runAsync(() -> System.out.println("values = " + values), executorService))
// since map() is a lazy operation, we need to use toList() to create the Future
.toList();
// join() method blocks the main thread until all Futures are completed
futures.forEach(CompletableFuture::join);
}

Parallel process collection in batches and return result using Java's Virtual Threads

int batchSize = 2;
List<String> input = List.of(
"apple",
"banana",
"orange",
"mango",
"peach"
);
List<List<String>> batches = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(index -> index / batchSize))
.values()
.stream()
.map(indices -> indices.stream().map(input::get).toList())
.toList();

try (ExecutorService executorService = Executors.newVirtualThreadPerTaskExecutor()) {
List<CompletableFuture<List<Integer>>> futures = batches.stream()
.map(values -> CompletableFuture
.supplyAsync(() -> values.stream().map(String::length).toList(), executorService))
// since map() is a lazy operation, we need to use toList() to create the Future
.toList();
List<List<Integer>> result = futures.stream()
// join() method blocks the main thread until all Futures are completed and returns the result
.map(CompletableFuture::join)
.toList();
result.forEach(integers -> System.out.println("result = " + integers));
}

IntStream essentials:

// end value excluded
IntStream.range(0, 2)
.boxed()
.forEach(integer -> System.out.println("integer = " + integer));

// end value included
IntStream.rangeClosed(0, 2)
.boxed()
.forEach(integer -> System.out.println("integer = " + integer));

// ascending iteration
IntStream.iterate(0, value -> value <= 2, operand -> operand + 1)
.boxed()
.forEach(integer -> System.out.println("integer = " + integer));

// ascending iteration with step of 2
IntStream.iterate(0, value -> value <= 5, operand -> operand + 2)
.boxed()
.forEach(integer -> System.out.println("integer = " + integer));

// descending iteration
IntStream.iterate(2, value -> value >= 0, operand -> operand - 1)
.boxed()
.forEach(integer -> System.out.println("integer = " + integer));

// descending iteration with step of 2
IntStream.iterate(5, value -> value >= 0, operand -> operand - 2)
.boxed()
.forEach(integer -> System.out.println("integer = " + integer));

Comments

Popular posts from this blog

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing

Colima - Drop In replacement for Docker Desktop for Mac and Linux