Thursday, September 25, 2025
HomeLanguagesJavaJava Guava | Lists.partition() method with Examples

Java Guava | Lists.partition() method with Examples

The Lists.partition() method in Guava Library is used to divide the original list into sublists of the same size. The method accepts two parameters.

For example: If the original list passed as parameter is [a, b, c, d, e] and the partition size is 3, then the sublists yield are as [[a, b, c], [d, e]].

Syntax:

public static <T> List<List<T>> partition(List<T> list, int size)

Parameters: The method accepts two parameters:

  • list: The list which is to be divided into sublists based on the partition size.
  • size: The desired size of each sublist. The size of the last sublist may be smaller.

Return Value: The method returns the list of consecutive sublists. Each sublist(except possibly the last one) has the size equal to the partition size.

Exception: The method Lists.partition() throws IllegalArgumentException if partition size is non-positive.

Below examples illustrate the implementation of above method:

Example 1:




// Java code to show implementation of
// Guava's Lists.partition() method
  
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a List of Integers
        List<Integer> myList
            = Arrays.asList(1, 2, 3, 4, 5);
  
        // Using Lists.partition() method to divide
        // the original list into sublists of the same
        // size, which are just views of the original list.
        // The final list may be smaller.
        List<List<Integer> > lists
            = Lists.partition(myList, 2);
  
        // Displaying the sublists
        for (List<Integer> sublist: lists)
            System.out.println(sublist);
    }
}


Output:

[1, 2]
[3, 4]
[5]

Example 2:




// Java code to show implementation of
// Guava's Lists.partition() method
  
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a List of Characters
        List<Character> myList
            = Arrays.asList('H', 'E', 'L', 'L', 'O',
                            'G', 'E', 'E', 'K', 'S');
  
        // Using Lists.partition() method to divide
        // the original list into sublists of the same
        // size, which are just views of the original list.
        // The final list may be smaller.
        List<List<Character> > lists
            = Lists.partition(myList, 3);
  
        // Displaying the sublists
        for (List<Character> sublist: lists)
            System.out.println(sublist);
    }
}


Output:

[H, E, L]
[L, O, G]
[E, E, K]
[S]

Reference: https://google.github.io/guava/releases/23.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-

RELATED ARTICLES

Most Popular

Dominic
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6682 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6753 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS