Site icon JavaGoal

removeFirst() method

removeFirst() method: This removefirst Java method is used to retrieve and remove the first element from LinkedList. Its return type is E.

E removeFirst();

Where, E is the element which you want to remove from the LinkedList.

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");
			
	String removedName = listOfNames.removeFirst();
	System.out.println("The element is removes successfully = "+removedName);
				
	for(String name : listOfNames)
	{
        	System.out.println("Names from list = "+name);
	}
   }
}

Output: The element is removes successfully = JAVA
Names from list = GOAL
Names from list = RAVI

Exit mobile version