pollFirst(): This pollfirst Java method is employed to removes and returns the first element from LinkedList. Its return type is E element. It Retrieve the element and also removes it from the LinkedList.
E pollFirst();
Where, E represents the type of elements in LinkedList.
return null, 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 first position System.out.println("Element present at first position is = "+listOfNames.pollFirst()); } }
Output: Element present at first position is = JAVA