ProLesson 7.6Intermediate2 min readUpdated 30 October 2026
Queue, Deque and PriorityQueue
Process items in order with queues, use ArrayDeque as a stack or queue, and always take the most important item next with PriorityQueue.
Queues hold items waiting to be processed: orders to ship, tasks to run, messages to send. Java gives you three main shapes.
01Queue: first in, first out
QueueDemo.java
import java.util.ArrayDeque;
import java.util.Queue;
public class QueueDemo {
public static void main(String[] args) {
Queue<String> orders = new ArrayDeque<>();
orders.offer("order-1"); // add to the back
orders.offer("order-2");
orders.offer("order-3");
System.out.println(orders.peek()); // look at the front
while (!orders.isEmpty()) {
System.out.println("Shipping " + orders.poll()); // take from the front
}
System.out.println(orders.poll()); // empty → null
}
}order-1 Shipping order-1 Shipping order-2 Shipping order-3 null
Each operation comes in two styles:
| Action | Throws if it fails | Returns a special value |
|---|---|---|
| Add | add | offer (returns false) |
| Remove the head | remove | poll (returns null) |
| Look at the head | element | peek (returns null) |
Loading the rest of the lesson…