Skip to content

Docs/java queue remove #7445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions content/java/concepts/queue/terms/remove/remove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
Title: 'remove()'
Description: 'Removes and returns the head (first element) of the queue, throwing an exception if the queue is empty.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Collections'
- 'Methods'
- 'Queues'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

Java's [`Queue`](https://www.codecademy.com/resources/docs/java/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`.

## Syntax

```pseudo
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:

```java
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:

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