Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

List size() method in Java with Examples

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.

Syntax:

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

Illustration: Assume it to be an integer list 

Input  : {3,2,1,5,7} 
Output : 5

Example 1:

Java




// Java program to Illustrate size() method
// of List class for Integer value
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] arg)
    {
        // Creating object of ArrayList class
        List<Integer> list = new ArrayList<Integer>();
 
        // Populating List by adding integer elements
        // using add() method
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
 
        // Printing elements of List
        System.out.println("Before operation: " + list);
 
        // Getting total size of list
        // using size() method
        int size = list.size();
 
        // Printing the size of List
        System.out.println("Size of list = " + size);
    }
}

Output

Before operation: [1, 2, 3, 4, 5]
Size of list = 5

Example 2:

Java




// Java program to Illustrate size() method
// of List class for Integer value
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty string list by
        // declaring elements of string type
        List<String> list = new ArrayList<String>();
 
        // Populating List by adding string elements
        // using add() method
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
 
        // Printing the List
        System.out.println("Before operation: " + list);
 
        // Getting total size of list
        // using size() method
        int size = list.size();
 
        // Printing the size of list
        System.out.println("Size of list = " + size);
    }
}

Output

Before operation: [Geeks, for, Geeks]
Size of list = 3

My Personal Notes arrow_drop_up
Last Updated : 24 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials