How to Partition List in Multiple Sublists in Java

A quick guide to Partitioning a List in Java to create multiple sublists of a specific size.

Overview

Partitioning a List means dividing a list into several sublists, also called partitions, where each partition contains a specific number of elements while the last partition can contain fewer elements.

Partitioning a List is mainly helpful in concurrent programming where instead of processing an extensive list sequentially, we can process small sublists in parallel using multiple threads.

This tutorial covers examples of dividing a Java List into multiple sublists of a given size using Guava, apache commons’ collections, and plain Java.

Use Guava to Partition a Java List

Before we use Guava to partition a List into multiple lists, please remember that the partitions the Guava creates are just views of the original List. Thus, any modifications to the original Java List will also reflect in the partitions.

Java List Partition Using Guava

The Lists class of the Guava Library provides the partition() method that accepts a Java List and the partition size. In response, the method returns a List containing individual sublists.

Example of partitioning a Java List using Guava

List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
List<List<Integer>> sublists = 
    Lists.partition(list, 3);

sublists.forEach(System.out::println);

//prints:
//[1, 2, 3]
//[4, 5, 6]
//[7]Code language: Java (java)

We got three partitions, each of the specified sizes except the last one.

As stated earlier, the partitions we received are just the original List’s views and will reflect the changes to the original List. For example, after creating the partitions, if we add three more elements to our List, our partition size grows by one. Consider our original List is mutable.

sublists.forEach(System.out::println);
//prints:
//[1, 2, 3]
//[4, 5, 6]
//[7]

list.addAll(List.of(11, 12, 13));
sublists.forEach(System.out::println);
//prints:
//[1, 2, 3]
//[4, 5, 6]
//[7, 11, 12]
//[13]Code language: Java (java)

Java Collection Partition using Guava

Similar to a List, we can use the partition() method in the Iterables class to partition a Java Iterable Collection. In response, the method returns an Iterable containing individual Lists.

Example of partitioning a Java Collection using Guava

Collection<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
Iterable<List<Integer>> partitions = 
    Iterables.partition(list, 3);

partitions.forEach(System.out::println);
//prints:
//[1, 2, 3]
//[4, 5, 6]
//[7]Code language: Java (java)

Java Collections Padded Partition using Guava

Sometimes, we may want all the partitions to be the same size. To do so, we can use a padded partition with which even the last partition has the same size.

Example of Padded Partitions of a Java List using Guava

Collection<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
Iterable<List<Integer>> partitions = 
    Iterables.paddedPartition(list, 4);

partitions.forEach(System.out::println);
//[1, 2, 3]
//[4, 5, 6]
//[7, null, null]Code language: Java (java)

Please note the last List is of size three, and the rest of the elements are padded with null values.

Use Apache Commons Collections to Partition a Java List

The Apache Commons Collections library provides ListUtils class with the static partition() method. This method takes a Java List and the partition size and returns a List containing individual sublists of the specified size.

Example of using Apache Commons Collections to Partition a Java List

List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
List<List<Integer>> partitions = 
    ListUtils.partition(list, 4);

partitions.forEach(System.out::println);
//prints:
//[1, 2, 3]
//[4, 5, 6]
//[7]Code language: Java (java)

Similar to the Guava, the partitions created by Apache Commons Collections library are views of the original List.

Using Plain Java to Partition a Java List

Previous examples of partitioning a Java List involved third-party libraries. However, we can create our partition method in Plain Java.

Example of Creating sublists from List in Plain Java.

public <T> List<List<T>> 
    partition(List<T> list, int size) {
  
  List<List<T>> partitions = new ArrayList<>();
  if (list.size() == 0) {
    return partitions;
  }

  int length = list.size();
  int numOfPartitions = length / size + ((length % size == 0) ? 0 : 1);

  for (int i = 0; i < numOfPartitions; i++) {
    int from = i * size;
    int to = Math.min((i * size + size), length);
    partitions.add(list.subList(from, to));
  }
  return partitions;
}Code language: Java (java)

The method’s signature is similar to the third-party method we saw earlier. Firstly, we calculate the size of the resulting partitions. Then we iteratively use the subList() method to break a List into a sublist of a given size.

Please note that the partitions we create here are actual Lists, not views. Thus, they won’t reflect any modifications to the original Java List.

Summary

We learned ways to break a Java List into multiple fixed-size sublists. In the examples, we used Guava and Apache Commons Collections libraries to partition a Java List and understood that the partitioned Lists are just the views of the original List. In the end, we saw how to use the subList() method in plain Java to break a List into multiple partitions of the same size.

You can refer to our GitHub Repository for the complete source code of the examples used in this tutorial.