A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/list-size-method-in-java-with-examples/ below:

List size() method in Java with Examples

List size() method in Java with Examples

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<Integer> l=new ArrayList<Integer>();
      
      	// 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.

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<Integer> l = new ArrayList<Integer>();

        // 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<String> l = new ArrayList<String>();

        // 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);
    }
}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4