HOWTO · Java

How to Split a List Into Chunks in Java

Learn how to split a Java list into fixed-size chunks with the JDK, streams, Apache Commons Collections, and Guava.

On this page

To split a Java list into fixed-size chunks without adding a dependency, advance through it by the chunk size and take each range with List.subList(). Stream collectors, Apache Commons Collections, and Guava offer alternatives when their result shape or an existing project dependency suits the task.

Use List.subList() to Split a List Into Chunks in Java

List.subList(fromIndex, toIndex) returns a view of a list range: the start is inclusive and the end is exclusive. Advance the start by the chunk size, and cap the end at the list length so the final chunk can be shorter. This complete example uses eight values and chunks of three. The loop ranges can be followed directly from the indices.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ChunkExample {
  public static void main(String[] args) {
    List<Integer> items = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80);
    int chunkSize = 3;
    List<List<Integer>> chunks = new ArrayList<>();

    for (int start = 0; start < items.size(); start += chunkSize) {
      int end = Math.min(start + chunkSize, items.size());
      chunks.add(items.subList(start, end));
    }

    System.out.println(chunks);
  }
}

The starts are 0, 3, and 6; the final end is capped at the list length. This keeps every value while allowing the last range to be shorter. With no input elements, the loop adds no chunks. The chunk size must be positive so the loop advances through the list.

Each result from subList() remains backed by the original list rather than becoming an independent copy. Treat the chunks as views if you need changes to reflect in the source list. Structural changes to the source list outside the view can make subsequent use of a sublist unreliable. To create independent chunk lists, copy each range when adding it, for example with new ArrayList<>(items.subList(start, end)).

Use Java Streams to Split a List Into Chunks in Java

A stream can generate starting indices and map each range to a sublist. One supplied form uses IntStream.iterate(0, i -> i < items.size(), i -> i + chunkSize) to produce indices. Another derives a range count from the list length and chunk size, then maps each index i to a range beginning at i * chunkSize. These forms produce the same ranges as the loop above; keep them in this section when choosing a stream-based approach.

For either form, the range count is zero when there are no input elements, and the chunk size must be positive before deriving the last index. No Java runtime execution evidence is supplied for these stream forms, so the index behavior is described from the APIs and arithmetic rather than as a tested result. The direct loop is easier to adapt when you want the bounds visible in one place.

Use Collectors.groupingBy() to Split a List Into Chunks in Java

Collectors.groupingBy() groups stream elements by a classification key. To form chunks, classify each element by its position divided by the chunk size, rather than by its value. With zero-based positions, indices 0 through chunkSize - 1 share one key, and the following range shares the next key.

For example, stream the indices and group them with Collectors.groupingBy(i -> i / chunkSize), then map each grouped index back to items.get(i). The result is a map from chunk number to a list of values. If the caller needs a list of lists, collect the map’s values into a list; do not rely on map iteration order unless the chosen map type and its ordering are part of the implementation. As with the other approaches, use a positive chunk size and account for an input with no elements before computing a range count.

Use Apache Commons Collections to Split a List Into Chunks in Java

If Apache Commons Collections is already a project dependency, its ListUtils.partition() method can partition a list using the list and chunk size as arguments: ListUtils.partition(items, chunkSize). Add the library to the build configuration before using this API; it is not part of the JDK. Consult the version selected by your project for its dependency coordinates and API details.

No library version, dependency fixture, or execution evidence is supplied here. This article therefore identifies the method but does not promise whether its returned partitions are views or copies, their mutability, or other version-specific behavior. Check the documentation for the exact Commons Collections version in your build when those details matter.

Use Google Guava to Split a List Into Chunks in Java

Guava offers the Lists.partition() method, called with a list and chunk size as Lists.partition(items, chunkSize). It is a dependency-based choice when Guava is already present in the application; otherwise, add the library through the project’s dependency manager before importing its collection API.

No Guava version or executable dependency fixture is supplied here, so this method is not presented as a tested example. Confirm the API and any behavior your application depends on against the Guava version selected by the project. The method name alone does not establish copy, view, mutability, or ordering guarantees for a particular version.

Conclusion

Use the index loop with List.subList() when a JDK-only solution and explicit range handling fit the task. Choose a stream form when index-based mapping fits the surrounding pipeline, or use Collectors.groupingBy() when a map keyed by chunk number is useful. Apache Commons Collections and Guava provide concise library alternatives when those dependencies already belong in the project. In every case, choose a positive chunk size, account for the final partial chunk, and check view or collection behavior against the API and dependency version you use.