Java remove()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 24, 2025
Contribute to Docs

Java’s Queue interface’s .remove() method removes and returns the element at the head (front) of the queue. If the queue is empty, it throws a NoSuchElementException.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax of Java .remove()

queue.remove()

Parameters:

The .remove() method does not take any parameters.

Return value:

Returns the element removed from the head of the queue.

Exception:

Throws NoSuchElementException exception if the queue is empty.

Note: Don’t confuse .remove() with remove(Object o), which is inherited from Collection and removes the first matching element from the queue if it exists.

Example: Removing the Head Element Using Java Queue .remove()

The example here uses a LinkedList implementation to demonstrate .remove() returning and deleting the head element:

import java.util.Queue;
import java.util.LinkedList;
public class RemoveExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("Alice");
queue.offer("Bob");
queue.offer("Charlie");
System.out.println("Queue before remove: " + queue);
String head = queue.remove(); // removes and returns "Alice"
System.out.println("Removed head: " + head);
System.out.println("Queue after remove: " + queue);
}
}

This results in the following output:

Queue before remove: [Alice, Bob, Charlie]
Removed head: Alice
Queue after remove: [Bob, Charlie]

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours