Skip to content
Dev Dump

Queue Interface

The Queue interface models a collection where elements are inserted at one end and removed from the other — typically FIFO (First-In-First-Out). The Deque (double-ended queue) interface extends it to support insertion and removal at both ends.

implmentation

Queue<E>
├── PriorityQueue<E> — heap-based, ordered by priority
├── Deque<E> (interface) — double-ended queue
│ ├── ArrayDeque<E> — resizable array (recommended for most uses)
│ └── LinkedList<E> — also implements List
└── BlockingQueue<E> — thread-safe, producer-consumer pattern
├── ArrayBlockingQueue — bounded
├── LinkedBlockingQueue — optionally bounded
├── PriorityBlockingQueue — unbounded, priority-ordered
└── SynchronousQueue — zero-capacity handoff

Queue provides two methods for each operation — one throws, one returns a special value:

OperationThrows on failureReturns null/false
Insertadd(e)offer(e)
Removeremove()poll()
Examineelement()peek()

Prefer offer/poll/peek — they fail gracefully.

queue expection-sepcial

Queue<String> queue = new ArrayDeque<>();
queue.offer("first");
queue.offer("second");
queue.offer("third");
queue.peek(); // "first" — looks at head without removing
queue.poll(); // "first" — removes and returns head
queue.poll(); // "second"
queue.size(); // 1
// Drain the queue
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}

Resizable circular array. Faster than LinkedList for both queue and stack use cases due to better cache locality.

Deque<String> deque = new ArrayDeque<>();
// Queue (FIFO)
deque.offerLast("a"); // add to tail
deque.pollFirst(); // remove from head
// Stack (LIFO)
deque.push("a"); // add to head
deque.pop(); // remove from head
// Both ends
deque.addFirst("start");
deque.addLast("end");
deque.peekFirst();
deque.peekLast();

Default initial capacity is 16. Pre-size with new ArrayDeque<>(expectedSize) if you know the count.

A min-heap that always gives you the smallest element first (by natural ordering or a Comparator). Not FIFO.

// Natural ordering (min first)
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(5);
pq.offer(2);
pq.offer(8);
pq.poll(); // 2
pq.poll(); // 5
pq.poll(); // 8
// Max-heap (largest first)
PriorityQueue<Integer> maxPQ = new PriorityQueue<>(Comparator.reverseOrder());
// Custom ordering
PriorityQueue<String> byLength = new PriorityQueue<>(
Comparator.comparingInt(String::length)
);

Key points:

  • offer/poll are O(log n), peek is O(1)
  • Iterator does NOT return elements in priority order — only poll does
  • Not thread-safe — use PriorityBlockingQueue for concurrency
ArrayDequeLinkedListPriorityQueueBlockingQueue
OrderingFIFOFIFOPriorityFIFO/Priority
offer/pollO(1)*O(1)O(log n)O(1) or O(log n)
MemoryLowHighLowMedium
Thread-safeNoNoNoYes
Use caseDefault queue/stackNeed List + QueueScheduling, top-KProducer-consumer

*Amortized

  • General-purpose queue or stackArrayDeque
  • Need both List and Queue methodsLinkedList
  • Process by priority, not insertion orderPriorityQueue
  • Multi-threaded producer-consumerArrayBlockingQueue or LinkedBlockingQueue