Skip to content

[Edit] Java: Queue #7335

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
221 changes: 188 additions & 33 deletions content/java/concepts/queue/queue.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,226 @@
---
Title: 'Queue'
Description: 'A Queue is a collection interface that offers additional operations for accessing or manipulating items at the head of the queue.'
Description: 'A linear data structure that stores and manages elements in First In, First Out order.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Algorithms'
- 'Collections'
- 'Queues'
- 'Data Structures'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

A **`Queue`** is an interface provided in the `java.util` package that extends collections to provide additional methods to access or manipulate items at the head of the queue. Where the "head" of the queue is defined by the particular implementation.

Queue elements are commonly accessed in a FIFO (first-in-first-out) manner. In a priority queue implementation, the first item out will be defined by its specified priority.
A **`Queue`** in Java is a linear [data structure](https://www.codecademy.com/resources/docs/general/data-structures) that stores and manages elements in a First In, First Out (FIFO) order. Elements are added to the rear of the queue and removed from the front, similar to how people wait in line at a store or bank. The queue follows the principle where the first element added is the first one to be removed.

## Syntax

Since `Queue` is an interface in Java, it cannot be instantiated directly. Instead, use implementing classes to create queue objects:

### Creating Queue Objects

```pseudo
Queue<DataType> queueName = new ImplementingClass<>();
```

**Parameters:**

- `DataType`: The type of elements the queue will store
- `queueName`: The name of the queue variable
- `ImplementingClass`: A class that implements the Queue interface (e.g., LinkedList, ArrayDeque, [PriorityQueue](https://www.codecademy.com/resources/docs/java/priorityqueue))

**Return value:**

`Queue` methods return different values based on success/failure scenarios as described above.

## Common Methods in the Queue Interface

- `add(element)`: Inserts element at the rear of the queue, throws exception if fails
- `offer(element)`: Inserts element at the rear of the queue, returns false if fails
- `remove()`: Removes and returns element from front, throws exception if empty
- `poll()`: Removes and returns element from front, returns null if empty
- `element()`: Returns front element without removing, throws exception if empty
- `peek()`: Returns front element without removing, returns null if empty

## Example 1: Basic Operations for `Queue` in Java

This example demonstrates fundamental queue operations including adding elements, removing elements, and accessing the front element:

```java
import java.util.Queue;
import java.util.LinkedList;

Queue<DataType> q = new QueueClass<DataType>();
```
public class BasicQueueExample {
public static void main(String[] args) {
// Create a queue using LinkedList implementation
Queue<String> customerQueue = new LinkedList<>();

Where [`DataType`](https://www.codecademy.com/resources/docs/java/data-types) is the data type to be stored in the queue, and `QueueClass` is some class implementing the `Queue` interface.
// Add customers to the queue
customerQueue.offer("Alice");
customerQueue.offer("Bob");
customerQueue.offer("Charlie");

## Methods
System.out.println("Queue: " + customerQueue);

The `Queue` interface utilizes the following methods:
// Serve the first customer (remove from front)
String servedCustomer = customerQueue.poll();
System.out.println("Served customer: " + servedCustomer);

- `.add(item)`: adds `item` to the `Queue` if possible, otherwise it throws an exception.
- `.offer(item)`: adds `item` to the `Queue` if possible, otherwise it returns `false`.
- `.remove()`: removes and returns the head item of the `Queue`, throwing an exception when the `Queue` is empty.
- `.poll()`: removes and returns the head item of the `Queue`, returning `null` if the `Queue` is empty.
- `.element()`: returns the head of the `Queue` without removing it, throwing an exception when the `Queue` is empty.
- `.peek()`: returns the head of the `Queue` without removing it, returning `null` if the `Queue` is empty.
// Check who is next without removing them
String nextCustomer = customerQueue.peek();
System.out.println("Next customer: " + nextCustomer);

## Example
System.out.println("Updated queue: " + customerQueue);
}
}
```

The output of this code is:

```shell
Queue: [Alice, Bob, Charlie]
Served customer: Alice
Next customer: Bob
Updated queue: [Bob, Charlie]
```

This example demonstrates how elements maintain their order in the queue. Alice, who was added first, is served first, maintaining the FIFO principle.

This is an example of the `Queue` interface implemented by a `LinkedList`:
## Example 2: Using `Queue` to Implement a Print Job Queue System

This example simulates a real-world scenario where a printer processes print jobs in the order they were submitted:

```java
import java.util.Queue;
import java.util.LinkedList;

public class PrintJobQueue {
public static void main(String[] args) {
// Create a queue to manage print jobs
Queue<String> printQueue = new LinkedList<>();

// Submit print jobs to the queue
printQueue.add("Document1.pdf");
printQueue.add("Presentation.pptx");
printQueue.add("Report.docx");
printQueue.add("Invoice.xlsx");

System.out.println("Print jobs in queue: " + printQueue.size());

// Process each print job in FIFO order
while (!printQueue.isEmpty()) {
String currentJob = printQueue.remove();
System.out.println("Printing: " + currentJob);

// Simulate printing time
try {
Thread.sleep(1000); // Wait 1 second
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

System.out.println("All print jobs completed!");
System.out.println("Queue status: " + (printQueue.isEmpty() ? "Empty" : "Has jobs"));
}
}
```

The output of this code is:

```shell
Print jobs in queue: 4
Printing: Document1.pdf
Printing: Presentation.pptx
Printing: Report.docx
Printing: Invoice.xlsx
All print jobs completed!
Queue status: Empty
```

This example shows how queues ensure fair processing where jobs are handled in the order they arrive, preventing newer jobs from jumping ahead of older ones.

## Example 3: Breadth-First Search Implementation Using `Queue`

This example demonstrates using a queue to implement breadth-first search in a tree structure, showcasing a common algorithmic use case:

```java
import java.util.Queue;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;

class TreeNode {
int val;
TreeNode left;
TreeNode right;

// Main.java
public class Main {
TreeNode(int val) {
this.val = val;
}
}

public class BreadthFirstSearch {
public static void main(String[] args) {
Queue<String> food = new LinkedList<String>();
food.offer("Cabbage");
food.offer("Pizza");
food.offer("Sausage");
food.offer("Potatoes");
food.offer("Salad");
System.out.println(food.peek());
System.out.println(food.poll());
System.out.println(food);
// Create a binary tree
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
root.right.right = new TreeNode(7);

List<Integer> result = breadthFirstTraversal(root);
System.out.println("BFS traversal: " + result);
}

public static List<Integer> breadthFirstTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;

// Queue to store nodes for level-by-level processing
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);

while (!queue.isEmpty()) {
TreeNode current = queue.poll();
result.add(current.val);

// Add child nodes to queue for next level processing
if (current.left != null) {
queue.offer(current.left);
}
if (current.right != null) {
queue.offer(current.right);
}
}

return result;
}
}
```

This will output the following:
The output of this code is:

```shell
Cabbage
Cabbage
[Pizza, Sausage, Potatoes, Salad]
BFS traversal: [1, 2, 3, 4, 5, 6, 7]
```

This example illustrates how queues enable level-by-level tree traversal, ensuring nodes are processed in breadth-first order rather than depth-first order.

## Frequently Asked Questions

### 1. What is the difference between `add()` and `offer()` methods?

Both insert elements, but `add()` throws an exception if insertion fails while `offer()` returns false.

### 2. When should I use `remove()` versus `poll()`?

Use `remove()` when you want an exception for empty queues, and `poll()` when you prefer null return values.

### 3. What are the main Queue implementations?

LinkedList (general-purpose), ArrayDeque (resizable array), and PriorityQueue (priority-based ordering).