List size() method in Java with Examples (original) (raw)

Last Updated : 11 Jul, 2025

The **size() method of the List interface in Java is used to get the number of elements in this list. That is, the list size() method returns the count of elements present in this list container.

**Example:

Java `

// Java Program to demonstrate // List size() Method import java.util.*;

class GFG { public static void main (String[] args) { // Declared List List l=new ArrayList();

      // Empty List
    System.out.println("Size : "+l.size());
}

}

`

**Syntax of Method

public int size()

**Parameters: This method does not take any parameters.

**Return Value: This method returns the **number of elements in this list.

list size method in java

Example of using List size() Method

**Example 1:

Java `

// Java program to Illustrate size() method // of List class for Integer value

import java.util.*;

public class GFG { public static void main(String[] arg) { // Creating object of ArrayList class List l = new ArrayList();

    // Adding Element
    l.add(1);
    l.add(2);
    l.add(3);
    l.add(4);
    l.add(5);

    // Getting total size of list
    // using size() method
    int s = l.size();

    System.out.println("Size : " + s);
}

}

`

**Example 2:

Java `

// Java program to Illustrate size() method // of List class for Integer value

import java.util.*;

public class GFG { public static void main(String[] args) { // Creating an empty string list by // declaring elements of string type List l = new ArrayList();

    // Populating List by adding string elements
    // using add() method
    l.add("Geeks");
    l.add("for");
    l.add("Geeks");

    // Getting total size of list
    // using size() method
    int size = l.size();

    // Printing the size of list
    System.out.println("Size : " + size);
}

}

`