How to count number of element in LinkedList

We can count number of element in LinkedList with the help of Size() Method . LinkedList size() method return number of element in the LinkdeList .

import java.util.LinkedList;

public class CountElement {

public static void main(String[] args) {

LinkedList<String> linkedList = new LinkedList<String>();

linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Before Remove Element Linklist is = "+linkedList);

//Returns the number of elements in this list.
System.out.println("number of element in linkedlist = "+linkedList.size());

}

}

Output:

Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]

number of element in linkedlist = 5

In this example we have seen how to count number of element in LinkedList. you can read java collection framework example about arraylist , set , map etc.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

57 − 53 =