How to use List size() method in Java With Examples?



In Java, a List is an interface which stores a sequence of elements of similar types. Since the list contains multiple elements, we might need to know how many element the list currently have.

To count the total number of elements present in a list, the List interface provides a method named size().

The List size() Method

The size() method of List interface is used to retrieve size of the current List. The size refers to total number of items currently the list holds. For example, a list have {1, 2, 3}, then the size of the list will be 3.

Note: This method can return maximum size equal to Integer.MAX_VALUE, even if the list size is equal or more than this.

Syntax of the size() Method

Following is the syntax of the size() method in Java:

int size()

Example 1: Get the size of a List

The following example shows the usage of the list size() method. We use this method to retrieve the total number of elements present in the list {10, 20, 30}:

import java.util.ArrayList;
import java.util.List;
public class sizeMethod  {
   public static void main(String[] args) {
      
      //instantiating a List using ArrayList class
      List<Integer> list = new ArrayList<>();

      //adding element to it
      list.add(10);
      list.add(20);
      list.add(30);

      System.out.println("The list elements are: " + list);

      //using size() method
      System.out.println("The size of list is: " + list.size());

   }
}

The above program produces the following output:

The list elements are: [10, 20, 30]
The size of list is: 3

Example 2: Get the size of a List after adding new Elements

This is another example of using the size() method to find the length of the list before and after adding the element to it:

import java.util.ArrayList;
import java.util.List;
public class sizeMethod  {
   public static void main(String[] args) {
      
      //instantiating a List using ArrayList class
      List<String> vowels = new ArrayList<>();
      
      System.out.println("List size before adding element to it: " + vowels.size());
      //adding element to it
      vowels.add("A");
      vowels.add("E");
      vowels.add("I");
      vowels.add("O");
      vowels.add("U");
      
      System.out.println("The list after adding elements: " + vowels);
      System.out.println("List size after adding elements to it: " + vowels.size());
   
   }
}

Below is the output of the above program:

List size before adding element to it: 0
The list after adding elements: [A, E, I, O, U]
List size after adding elements to it: 5

Example 3: Check the Empty List

In this example, we will compare the result of size() with the value 0, and if both are equal, it means the current list is empty("").

Because if the list is empty, the size() method will always return "0".

import java.util.ArrayList;
import java.util.List;
public class sizeMethod  {
   public static void main(String[] args) {
      
      //instantiating a List using ArrayList class
      List<String> list = new ArrayList<>();
      
      System.out.println("The list elements are: " + list);
      
      //using size() method
      int result = list.size();
      
      //compare result with 0
      if(result == 0){
         System.out.println("List is empty.!!");
      }
      else{
         System.out.println("List is not empty.!!");
      }
   
   }
}

Following is the output of the above program:

The list elements are: []
List is empty.!!
Updated on: 2025-05-26T19:39:42+05:30

548 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements