Open In App
Related Articles

LinkedList size() Method in Java

Improve Article
Save Article
Like 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.



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 illustrates the Java.util.LinkedList.size() method:

Example 1: 


// Java code to illustrate size() method
// of LinkedList class
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
        // Displaying the linkedlist
        System.out.println("LinkedList:" + list);
        // Displaying the size of the list
            "The size of the linked list is: "
            + list.size());

In Java, the LinkedList class provides the size() method to get the number of elements in the list. The method returns an integer representing the number of elements in the list.

Example 2:


import java.util.LinkedList;
public class Example {
    public static void main(String[] args)
        LinkedList<String> list = new LinkedList<>();
        // Adding elements to the list
        // using add() method
        System.out.println("Original list:");
        // get the size of the list
        int size = list.size();
        System.out.println("Size of list: " + size);


Original list:
[apple, banana, orange, grape]
Size of list: 4

In this example, we first create a LinkedList object and add four elements to it. The System.out.println() statement prints the original list to the console.

We then use the size() method to get the number of elements in the list, and print the size to the console.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 31 Mar, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials