Partition a list into multiple sublists in Java
This post will discuss how to partition a list into multiple sublists in Java using Java Collections, Guava library, and Apache Common Collections.
In the previous post, we have discussed how to partition a list into two sublists in Java. This post will discuss how to partition a list into multiple sublists.
1. Naive solution
A naive solution is to create m empty lists and process each element of the original list, and add it to the corresponding sublist based on its position in the original list, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Generic method to partition a list into sublists of size `n` each in Java // (The final list might have fewer items) public static<T> List[] partition(List<T> list, int n) { // get the size of the list int size = list.size(); // Calculate the total number of partitions `m` of size `n` each int m = size / n; if (size % n != 0) { m++; } // create `m` empty lists List<T>[] partition = new ArrayList[m]; for (int i = 0; i < m; i++) { partition[i] = new ArrayList(); } // process each list element and add it to the corresponding // list based on its position in the original list for (int i = 0; i < size; i++) { int index = i / n; partition[index].add(list.get(i)); } // return the lists return partition; } |
2. Using List.subList() method
List interface provides the subList() method that returns a sublist between the specified indexes, backed by the list. We can use this method to partition our list into multiple sublists, but since the returned sublists are just views of the original list, we can construct new lists from the returned views, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// Generic method to partition a list into sublists of size `n` each // in Java using `List.subList()` (The final list might have fewer items) public static<T> List[] partition(List<T> list, int n) { // get the size of the list int size = list.size(); // Calculate the total number of partitions `m` of size `n` each int m = size / n; if (size % n != 0) { m++; } // create `m` empty lists and initialize them using `List.subList()` List<T>[] partition = new ArrayList[m]; for (int i = 0; i < m; i++) { int fromIndex = i*n; int toIndex = (i*n + n < size) ? (i*n + n) : size; partition[i] = new ArrayList(list.subList(fromIndex, toIndex)); } // return the lists return partition; } |
3. Using Guava Library
With the Guava library, we can use the Lists.partition() method that partitions the list into consecutive sublists, each of the specified size.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Generic method to partition a list into sublists of size `n` each // in Java using Guava (The final list might have fewer items) public static<T> List[] partition(List<T> list, int n) { // Calculate the total number of partitions of size `n` each int m = list.size() / n; if (list.size() % n != 0) { m++; } // partition the list into sublists of size `n` each List<List<T>> itr = Lists.partition(list, n); // create `m` empty lists and initialize them with sublists List<T>[] partition = new ArrayList[m]; for (int i = 0; i < m; i++) { partition[i] = new ArrayList(itr.get(i)); } // return the lists return partition; } |
Guava’s Iterables class contains a static utility method partition(Iterable<T>, int) that divides an iterable into unmodifiable sublists of the given size. We can use this method to partition our list into multiple sublists, but since the returned sublists are unmodifiable, we can construct new mutable lists from the returned sublists.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Generic method to partition a list into sublists of size `n` each // in Java using Guava (The final list might have fewer items) public static<T> List[] partition(List<T> list, int n) { // Calculate the total number of partitions of size `n` each int m = list.size() / n; if (list.size() % n != 0) { m++; } // partition the list into sublists of size `n` each and get an iterator Iterator<List<T>> itr = Iterables.partition(list, n).iterator(); // create `m` empty lists List<T>[] partition = new ArrayList[m]; for (int i = 0; itr.hasNext(); i++) { partition[i] = new ArrayList(itr.next()); } // return the lists return partition; } |
4. Using Apache Commons Collections
Apache Commons Collections also provides the ListUtils.partition() method that has exact functionality as Guava’s Lists.partition() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Generic method to partition a list into sublists of size `n` each in Java // using Apache Common Collections (The final list might have fewer items) public static<T> List[] partition(List<T> list, int n) { // Calculate the total number of partitions of size `n` each int m = list.size() / n; if (list.size() % n != 0) { m++; } // partition the list into sublists of size `n` each List<List<T>> itr = ListUtils.partition(list, n); // create `m` empty lists and initialize them with sublists List<T>[] partition = new ArrayList[m]; for (int i = 0; i < m; i++) { partition[i] = new ArrayList(itr.get(i)); } // return the lists return partition; } |
That’s all about partitioning a list into multiple sublists in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)