Skip to content
Related Articles

Related Articles

LinkedList size() Method in Java

View Discussion
Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 10 Dec, 2018
View Discussion
Improve Article
Save Article

The Java.util.LinkedList.size() method is used to get the size of the Linked list or the number of elements present in the linked list.

Syntax:

LinkedList.size()

Parameters: This method does not take any parameter.

Return Value: This method returns the size or the number of elements present in the LinkedList.

Below program illustrate the Java.util.LinkedList.size() method:




// Java code to illustrate size()
import java.io.*;
import java.util.LinkedList;
  
public class LinkedListDemo {
    public static void main(String args[])
    {
  
        // Creating an empty LinkedList
        LinkedList<String> list = new LinkedList<String>();
  
        // Using add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("10");
        list.add("20");
  
        // Displaying the linkedlist
        System.out.println("LinkedList:" + list);
          
        // Displaying the size of the list
        System.out.println("The size of the linked list is: " 
                                                + list.size());
    }
}

Output:

LinkedList:[Geeks, for, Geeks, 10, 20]
The size of the linked list is: 5
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!