getLast() method: This getLast java method employs to get the last element from LinkedList. It returns the object.
E getLast();
Where, E represents the type of elements in LinkedList.
It throws NoSuchElementException if this LinkedList is empty.
import java.util.LinkedList; public class ExampleOfLinkedList { public static void main(String[] args) { LinkedList<String> listOfNames = new LinkedList<String>(); listOfNames.add("JAVA"); listOfNames.add("GOAL"); listOfNames.add("RAVI"); // It returns the element present at index 1 System.out.println("Element present at last position is = "+listOfNames.getLast()); } }
Output: Element present at last position is = RAVI