LinkedList getLast() Method in Java Last Updated : 18 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 class Geeks { public static void main(String args[]) { // Create an empty list LinkedList<String> l = new LinkedList<>(); // Use add() to add // elements in the list l.add("Geeks"); l.add("for"); l.add("Geeks"); System.out.println("" + l); // Use getLast() to display the // last element of the list System.out.println("" + l.getLast()); } } Output[Geeks, for, Geeks] Geeks Syntax of LinkedList getLast() Methodpublic E getLast()Return type: It return the last element of the list. Example 2: Here, the getLast() method throws an NoSuchElementException if the list is empty. Java // Throws NoSuchElementException import java.util.LinkedList; class Geeks { public static void main(String[] args) { // Create an empty list LinkedList<Integer> l = new LinkedList<>(); // Use add() to add // elements in the list l.add(10); l.add(20); l.add(30); l.add(40); l.add(50); // Use getLast() to get the // last element of the list System.out.println("The last element of the LinkedList is: " + l.getLast()); // Use clear() to clear the list l.clear(); // Trying to get the last // element from an empty list try { System.out.println( "Last element of LinkedList is: " + l.getLast()); } catch (Exception e) { System.out.println("Error: " + e); } } } OutputThe last element of the LinkedList is: 50 Error: java.util.NoSuchElementException Comment More infoAdvertise with us Next Article LinkedList getLast() 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 add() Method in Java In Java, the add() method of the LinkedList class is used to add an element to the list. By default, it adds the element to the end of the list, if the index is not specified.Example: Here, we use the add() method to add a single element to the list.Java// Java program to add elements in LinkedList 2 min read LinkedList set() Method in Java The Java.util.LinkedList.set() method is used to replace any particular element in the linked list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: Link 3 min read LinkedList remove() Method in Java In Java, the remove() method of the LinkedList class removes an element from the list, either by specifying its index or by providing its value.Example 1: Here, we use the remove() method to remove element from the LinkedList of Strings. By default the remove() will remove the beginning element(head 3 min read LinkedList get() Method in Java In Java, the get() method of LinkedList is used to fetch or retrieve an element at a specific index from a LinkedList.Example 1: Here, we use the get() method to retrieve an element at a specified index.Java// Java Program to illustrate get() method import java.util.LinkedList; public class Geeks { 2 min read LinkedList addAll() Method in Java In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an 3 min read LinkedList addFirst() Method in Java In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning.Syntax of LinkedList addFirst() Method public void addFirst( E e)Parameters: E is the data type of elemen 1 min read LinkedList addLast() Method in Java In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list.Syntax of LinkedList addLast() Method void addLast( E e)Parameter: e is the element you want to add at the end of the list.Return type: This method does not return any value.Example: Here, we use t 1 min read LinkedList clear() Method in Java In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty.Syntax of LinkedList clear() Methodvoid clear()Parameters: This method does not accept any para 1 min read LinkedList clone() Method in Java In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list.Syntax of Java LinkedList clone() Method 1 min read LinkedList contains() Method in Java In Java, the contains() method of LinkedList is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list.Syntax of Java LinkedList contains() Method boolean contains(Object element);Parameter: The p 2 min read Like