LinkedList size() Method in Java Last Updated : 31 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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 illustrates the Java.util.LinkedList.size() method: Example 1: Java // Java code to illustrate size() method // of LinkedList class 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()); } } 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: Java 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 list.add("apple"); list.add("banana"); list.add("orange"); list.add("grape"); System.out.println("Original list:"); System.out.println(list); // get the size of the list int size = list.size(); System.out.println("Size of list: " + size); } } OutputOriginal 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. Comment More infoAdvertise with us Next Article LinkedList size() Method in Java C chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-LinkedList +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads LinkedList getLast() Method in Java In Java, the getLast() method in the LinkedList class is used to retrieve the last element of the list.Example 1: Here, we use the getLast() method to retrieve the last element of the list.Java// Java Program to Demonstrate the // use of getLast() in LinkedList import java.util.LinkedList; public cl 2 min read LinkedList indexOf() Method in Java In Java, the indexOf() method of the LinkedList class is used to find the index of the first occurrence of the specified element in the list. Syntax of LinkedList indexOf() Method public int indexOf(Object o);Parameter: This method takes an object "o" as an argument.Return type: It returns the index 2 min read LinkedList lastIndexOf() Method in Java In Java, the lastIndexOf() method of LinkedList is used to find the last occurrence of the specified element in the list. If the element is present it will return the last occurrence of the element, otherwise, it will return -1.Syntax of LinkedList lastIndexOf() Method public int lastIndexOf(Object 2 min read LinkedList listIterator() Method in Java In Java, the listIterator() method of the LinkedList class returns a ListIterator that allows us to iterate over the elements of the list.Example: Java// Java Program to Demonstrate the // use of listIterator() in LinkedList import java.util.LinkedList; import java.util.ListIterator; public class Ge 3 min read Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java In Java, the LinkedList class is present in java.util package, and it also has different methods that do the work of flexible addition of elements and help addition both at the front and back of the list. In this article, we cover the LinkedList offer(), offerFirst(), and offerLast() methods. These 5 min read Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java Linked list class offers the functionality to "look into" the first and last elements of the list and hence can be useful in cases where only the retrieval is required and not necessarily the deletion is required. Three functionalities are present and all are discussed in this article. 1. peek() : T 4 min read Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java Java's Linked list class offers a function that allows a "Queue Based" working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3 4 min read LinkedList pop() Method in Java In Java, the pop() method of the LinkedList class is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to the removeFirst method in LinkedList.Example 1: Here, we use t 2 min read LinkedList push() Method in Java In Java, the push() method of the LinkedList class is used to push an element at the starting(top) of the stack represented by LinkedList. This is similar to the addFirst() method of LinkedList. This method simply inserts the element at the first position or top of the LinkedList.Syntax of LinkedLis 1 min read LinkedList removeFirst() Method in Java In Java, the removeFirst() method of the LinkedList class is used to remove and return the first element of the list.Example 1: Here, we use the removeFirst() method to remove the first element (head) of the LinkedList of Integers.Java// Java Program to demonstrate the // use of removeFirst() in Lin 2 min read Like