Skip to content
Veltoryx Technologies
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
    }
}
Expected output
order-1
Shipping order-1
Shipping order-2
Shipping order-3
null

Each operation comes in two styles:

ActionThrows if it failsReturns a special value
Addaddoffer (returns false)
Remove the headremovepoll (returns null)
Look at the headelementpeek (returns null)

Loading the rest of the lesson…