Stop 08 · Collections & GenericsTopics · 6/10

Queue, Deque & PriorityQueue

Part of the Java Roadmap

In short

Print jobs, customer support tickets, undo history, hospital emergencies: some things are served in arrival order, some in reverse, and some by priority. Java has a ready-made structure for each.

Queue is first-in, first-out, Deque supports both ends (ArrayDeque is the preferred stack and queue), and PriorityQueue always gives you the smallest (or highest-priority) element first.

The jaadu manages a railway ticket line, lifts the top plate off a tall stack, and waves the most injured patient forward at a hospital desk.

Think of it like this

Railway counter, plate stack and hospital triage

A railway ticket counter serves whoever came first (Queue). A stack of plates at a wedding buffet gives you the plate placed last (stack, done with a Deque). The emergency ward sees the most serious patient first, no matter who arrived when (PriorityQueue).

Watch it happen

A ticket counter as a queue

enqueue(Ananya)
Ananya

Front of the queue is coloured

1/5offer("Ananya"): she joins at the back of the line.

The big ideas

What you need to know

Queue is FIFO

offer() adds at the back, poll() removes from the front, peek() looks without removing.

Deque works both ends

addFirst, addLast, pollFirst, pollLast. Use push and pop to treat it as a stack.

ArrayDeque is the default

It beats LinkedList as a queue and replaces the old Stack class, which is synchronized and slower.

PriorityQueue serves the smallest

It is a binary heap. poll() returns the smallest element, or the top one by your Comparator, in O(log n).

See it in code

Walk through the code

java
1import java.util.ArrayDeque;2import java.util.Comparator;3import java.util.Deque;4import java.util.PriorityQueue;5import java.util.Queue;6 7public class QueueDemo {8    record Patient(String name, int severity) {}9 10    public static void main(String[] args) {11        Queue<String> counter = new ArrayDeque<>();12        counter.offer("Ananya");13        counter.offer("Rohit");14        System.out.println(counter.poll());   // Ananya (first in, first out)15 16        Deque<String> history = new ArrayDeque<>();17        history.push("home");18        history.push("cart");19        System.out.println(history.pop());    // cart (last in, first out)20 21        PriorityQueue<Patient> er = new PriorityQueue<>(22            Comparator.comparingInt(Patient::severity).reversed());23        er.offer(new Patient("Vikram", 3));24        er.offer(new Patient("Sana", 9));25        er.offer(new Patient("Kiran", 5));26        System.out.println(er.poll().name()); // Sana, most severe first27    }28}

1/4As a Queue, ArrayDeque serves the earliest arrival.

One ArrayDeque used as a queue, another as a stack, and a PriorityQueue ordered by severity, highest first.

Save yourself the pain

Common mistakes

Oops

Using the legacy Stack class for a stack.

Do this instead

Use Deque<T> stack = new ArrayDeque<>() with push, pop and peek.

Oops

Printing a PriorityQueue and expecting it to look sorted.

Do this instead

Only the head is guaranteed to be the smallest. Call poll() repeatedly to get elements in order.

Oops

Calling remove() or element() on an empty queue and getting an exception.

Do this instead

Use poll() and peek(), which return null when the queue is empty.

Your mission

Support desk simulator

~30 min

Quick check

Which class is recommended for both stack and queue behaviour in modern Java?

Go deeper

  1. 1Use ArrayDeque as a stack with push() and pop().
  2. 2Use a PriorityQueue to find the k largest numbers in a list.
  3. 3Learn the difference between offer/poll/peek and add/remove/element.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge