Site icon JavaGoal

poll method in java

poll(): This poll method in java is utilized to removes and return the first element from the LinkedList. Its return type is E. It Retrieves the element and also removes it from LinkedList.

E poll();

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.poll());
	}
}

Output: Element present at first position is = JAVA

Exit mobile version