Open In App

LinkedList removeFirst() Method in Java

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, the LinkedList class provides the removeFirst() method to remove and return the first element of the list. If the list is empty, the method throws a NoSuchElementException.

Sure, here is an example of using the removeFirst() method in Java, with the full code and output:

Java




import java.util.LinkedList;
 
public class Example {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
 
        // add elements to the list
        list.add("apple");
        list.add("banana");
        list.add("orange");
        list.add("grape");
        System.out.println("Original list:");
        System.out.println(list);
 
        // remove the first element
        String removed = list.removeFirst();
        System.out.println("Element removed: " + removed);
        System.out.println(list);
    }
}


Output

Original list:
[apple, banana, orange, grape]
Element removed: apple
[banana, orange, grape]

In this example, we first create a LinkedList object and add four elements to it. The System.out.println() statement prints the original list to the console.

We then use the removeFirst() method to remove the first element from the list (which is “apple”), and print the removed element and the updated list to the console.

The Java.util.LinkedList.removeFirst() method is used to remove the first element from a linked list. This function also returns the first element after removing it. Syntax:

LinkedList.removeFirst();

Parameters: This function does not take any parameters. 

Return Value: The method returns the first element or the element present at the head of the list. 

Below program illustrates the Java.util.LinkedList.removeFirst() method: 

Java




// Java code to illustrate removeFirst() method
 
import java.io.*;
import java.util.LinkedList;
 
public class LinkedListDemo {
    public static void main(String args[])
    {
        // Creating an empty LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // Using add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("10");
        list.add("20");
 
        // Displaying the list
        System.out.println("LinkedList:" + list);
 
        // Remove the head using removeFirst() method
        System.out.println("The first element is: "
                           + list.removeFirst());
 
        // Displaying the final list
        System.out.println("Final LinkedList:" + list);
    }
}


Output

LinkedList:[Geeks, for, Geeks, 10, 20]
The first element is: Geeks
Final LinkedList:[for, Geeks, 10, 20]

Example:

Java




// Java code to illustrate removeFirst() method
 
import java.io.*;
import java.util.LinkedList;
 
public class LinkedListDemo {
 
    public static void main(String args[])
    {
        // Creating an empty LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // Using add() method to add elements in the list
        list.add("Geeks");
        list.add("for");
        list.add("Geeks");
        list.add("10");
        list.add("20");
 
        // Displaying the list
        System.out.println("LinkedList:" + list);
 
        // Remove the head using removeFirst() method
        System.out.println("The first element is: "
                           + list.removeFirst());
 
        // Displaying the final list
        System.out.println("Final LinkedList:" + list);
    }
}


Output

LinkedList:[Geeks, for, Geeks, 10, 20]
The first element is: Geeks
Final LinkedList:[for, Geeks, 10, 20]


Previous Article
Next Article

Similar Reads

ArrayDeque removeFirst() Method in Java
The Java.util.ArrayDeque.removeFirst() method is used to remove the first element of the Deque. Syntax: Array_Deque.removeFirst() Parameters: The method does not take any parameters. Return Value: This method returns the first element of the Deque after removing it. Exceptions: The method throws NoSuchElementException is thrown if the deque is empt
2 min read
LinkedBlockingDeque removeFirst() method in Java
The removeFirst() method of LinkedBlockingDeque returns and removes the first element of the Deque container from it. The method throws an NoSuchElementException if the Deque container is empty. Syntax: public E removeFirst() Returns: This method returns the head of the Deque container, which is the first element. Exception: The function throws a N
2 min read
ConcurrentLinkedDeque removeFirst() method in Java
The ConcurrentLinkedDeque.removeFirst() is an in-built function in Java which removes the first element from the deque container. The function throws a NoSuchElementException if this deque is empty. Syntax: Conn_Linked_Deque.removeFirst() Parameters: The function does not accepts any parameter. Return Values: The function returns the first element
2 min read
Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
Linked list class offers the functionality to "look into" the first and last elements of the list and hence can be useful in cases where only the retrieval is required and not necessarily the deletion is required. Three functionalities are present and all are discussed in this article. 1. peek() : This method retrieves, but does not remove, the hea
4 min read
Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
Linked list also has a function that does the work of flexible addition of elements and helps addition both at front and back of the list, these functions literally "offer" the facility and named offer(). Three types are available and are discussed in this very article below. 1. offer(E e) : This method adds the specified element as the tail (last
4 min read
Java.util.LinkedList.get(), getFirst(), getLast() in Java
In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first element has an index of 0.getFirst(): This method r
5 min read
Java.util.LinkedList.indexOf(), lastIndexof() in Java
Linked list library also offers to depict the first and last index of element that has to be found using the indexOf() and lastIndexOf() functions respectively. They offer a variety as the direct access is not available in the conventionally made linked list, hence knowledge of it is useful. 1. indexOf(Object o) : This method returns the index of t
4 min read
Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
Java's Linked list class offers a function that allows a "Queue Based" working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3 variants of poll(), all three are discussed in th
4 min read
LinkedList add() Method in Java With Examples
LinkedList is a class implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. As we all know that class contains various methods so do here we will be discussing and implementing add() method t
3 min read
LinkedList getFirst() Method in Java
Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The Java.util.LinkedList.getFirst() meth
2 min read