All topics
library
intermediate

PriorityQueue & Deque

Use priority-based and double-ended queue implementations for scheduling, BFS, and stack/queue patterns.

Two queues with very different ordering.

PriorityQueue = hospital ER triage (most urgent patient seen first, not first-come-first-served). Deque = a double-door hallway (enter/exit from either end).

Key Concepts

1
    PriorityQueue   smallest element first   a binary min-heap
    ArrayDeque      both ends                a circular array
2
PriorityQueue is not FIFO. poll() always returns the smallest:
PriorityQueuepoll()
3
    var q = new PriorityQueue<Integer>();
    q.addAll(List.of(5, 1, 3));
    q.poll();            // 1
    q.poll();            // 3
4
    // largest first
    new PriorityQueue<>(Comparator.reverseOrder());
    new PriorityQueue<>(Comparator.comparingInt(Task::priority));
5
Costs: offer and poll are O(log n), peek is O(1).
offerpollpeek
6
The trap: iteration is not sorted.
    for (int n : q) print(n);     // 1 5 3 -- heap order, not sorted
7
Only peek and poll respect the ordering. Printing a PriorityQueue directly shows the array layout, which looks like a bug and is not. To get sorted output, poll until empty.
peekpollPriorityQueue
8
ArrayDeque is both a stack and a queue, and faster than the alternatives.
    // as a stack (LIFO)
    deque.push(x);  deque.pop();  deque.peek();
ArrayDeque
9
    // as a queue (FIFO)
    deque.offer(x);  deque.poll();  deque.peek();
10
    // as a deque
    offerFirst / offerLast    pollFirst / pollLast
11
Use it instead of Stack and LinkedList.
    Stack          synchronized on every call, and extends Vector
    LinkedList     a node object plus two pointers per element
    ArrayDeque     one array, no locking
StackLinkedList
12
Stack is effectively deprecated by the Javadoc itself, which points at Deque.
StackDeque
13
ArrayDeque rejects null. That is deliberate: poll returns null for an empty deque, so a stored null would be indistinguishable from empty. LinkedList allows it and inherits exactly that ambiguity.
ArrayDequenullpollLinkedList
14
Where each shows up. PriorityQueue for schedulers, Dijkstra and merging sorted streams; ArrayDeque for breadth-first search, undo stacks and sliding windows.
PriorityQueueArrayDeque
15
What the interviewer is probing.1. "You iterate a PriorityQueue and the output is not sorted. Why?" Probing: what the heap guarantees. Stalls: "It is broken." Moves up: only peek and poll respect the ordering; iteration exposes the array layout of the heap. Poll until empty for sorted output.
16
2. "Why use ArrayDeque instead of Stack?" Probing: whether you know Stack is discouraged. Stalls: "Stack is fine." Moves up: Stack extends Vector and synchronises every call; the Javadoc itself points at Deque, and ArrayDeque is faster with no locking.
17
3. "Why does ArrayDeque reject null?" Probing: the ambiguity it avoids. Stalls: "An oversight." Moves up: poll returns null for an empty deque, so a stored null would be indistinguishable from empty — LinkedList allows it and inherits exactly that problem.
18
4. "How do you make a PriorityQueue return the largest first?" Probing: the comparator. Stalls: "Reverse the list." Moves up: pass Comparator.reverseOrder, or a comparator on the field you want ordered.