How do I get length of list of lists in Java?



In Java, a list is a collection that we use for storing elements. In many cases, we need to get the length of a list of lists. In this article, we will learn how to get the length of a list of lists in Java.

List provides a method size() to get the count of elements present in the current list. To get the size of each list, we can iterate through each item in the list and add its size to get the count of all elements present in the list of lists.

Ways to Get the Length of a List of Lists in Java

Using a For Loop

We can use a for loop to iterate through each list in the list of lists and get the size of each list using the size() method.

Example

In the below example, we will create 3 lists of lists and then use a for loop to get the length of each list.

import java.util.ArrayList;
import java.util.List;
public class LengthOfListOfLists {
   public static void main(String[] args){
      List<List<Integer>> listOfLists = new ArrayList<>();
      List<Integer> list1 = new ArrayList<>();
      list1.add(1); 
      list1.add(2);
      List<Integer> list2 = new ArrayList<>();
      list2.add(3);
      list2.add(4);
      list2.add(5);
      List<Integer> list3 = new ArrayList<>();
      list3.add(6);
      listOfLists.add(list1);
      listOfLists.add(list2);
      listOfLists.add(list3);

      for(List<Integer> list : listOfLists) {
         System.out.println("Length of list: " + list.size());
      }
   }
}

Output

Following is the output of the above code:

Length of list: 2
Length of list: 3
Length of list: 1

Using Stream API

We can also use the Stream API to get the length of each list in the list of lists. We can use the map() method to map each list to its size and then collect the results into a new list.

Example

In the below example, we will create a list that contains 3 lists and then use the Stream API to get the length of each list with their name.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class LengthOfListOfLists {
   public static void main(String[] args){
      List<List<Integer>> listOfLists = new ArrayList<>();
      List<Integer> list1 = new ArrayList<>();
      list1.add(1); 
      list1.add(2);
      List<Integer> list2 = new ArrayList<>();
      list2.add(3);
      list2.add(4);
      list2.add(5);
      List<Integer> list3 = new ArrayList<>();
      list3.add(6);
      listOfLists.add(list1);
      listOfLists.add(list2);
      listOfLists.add(list3);

      List<Integer> lengths = listOfLists.stream()
         .map(List::size)
         .collect(Collectors.toList());
      
      System.out.println("Lengths of lists: " + lengths);
   }
}

Output

Following is the output of the above code:

Lengths of lists: [2, 3, 1]
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-06-05T14:39:21+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements