Java Utility Library

Java LinkedList - size() Method



The java.util.LinkedList.size() method returns the total number of elements in the list.

Syntax

public int size()

Parameters

No parameter is required.

Return Value

Returns the number of elements in the list.

Exception

NA

Example:

In the example below, the java.util.LinkedList.size() method is used to find out the number of elements in the list.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a linkedlist
    LinkedList<Integer> MyList = new LinkedList<Integer>();

    //populating linkedlist
    MyList.add(10);
    MyList.add(20);
    MyList.add(30);

    System.out.println("Size of MyList: "+ MyList.size());    

    //adding one more element in the linkedlist
    MyList.add(40); 

    System.out.println("Now, Size of MyList: "+ MyList.size());  
  }
}

The output of the above code will be:

Size of MyList: 3
Now, Size of MyList: 4

❮ Java.util - LinkedList