Java Queue, Deque & ArrayDeque: FIFO Buffers & Priority Queues

The Queue interface models a collection designed for holding elements prior to processing, typically following FIFO (First-In, First-Out) ordering. The Deque (Double-Ended Queue) interface extends Queue to permit insertion, deletion, and inspection from both ends.

The 2 Sets of Queue Methods: Exceptions vs. Special Values

Operation Throws Exception on Failure Returns Special Value (false / null)
Insert add(e) (Throws IllegalStateException if full) offer(e) (Returns false if full)
Remove remove() (Throws NoSuchElementException if empty) poll() (Returns null if empty)
Examine element() (Throws NoSuchElementException if empty) peek() (Returns null if empty)

1. PriorityQueue (Binary Min-Heap)

import java.util.PriorityQueue;

public class PriorityQueueDemo {
    public static void main(String[] args) {
        // Natural ordering: Min-Heap (Lowest integer has highest priority)
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        pq.offer(40);
        pq.offer(10);
        pq.offer(30);
        pq.offer(5);

        // Elements polled strictly in priority order:
        while (!pq.isEmpty()) {
            System.out.print(pq.poll() + " "); // Output: 5 10 30 40
        }
    }
}

2. ArrayDeque: High-Speed Stack & Queue

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
    public static void main(String[] args) {
        // Using ArrayDeque as a Modern, High-Speed LIFO Stack:
        Deque<String> stack = new ArrayDeque<>();
        stack.push("Bottom Plate");
        stack.push("Middle Plate");
        stack.push("Top Plate");

        System.out.println("Popped: " + stack.pop()); // Top Plate
    }
}

Next Step

Now let us explore Iterators, ListIterator, and Fail-Fast vs Fail-Safe mechanics. Proceed to: Java Iterators →

Frequently Asked Questions (FAQ)

Q1. What is the difference between Queue and Deque in Java?
A Queue is a First-In-First-Out (FIFO) structure where elements are added at the tail and removed from the head. A Deque (Double-Ended Queue) permits insertion, inspection, and removal at BOTH ends (head and tail), functioning seamlessly as both a FIFO queue and a LIFO stack.
Q2. How does PriorityQueue order elements in Java?
PriorityQueue orders elements according to priority using a Binary Min-Heap (the smallest element is always at the head). Custom priority orders can be defined using a Comparator.
Q3. Why is ArrayDeque preferred over Stack and LinkedList?
ArrayDeque is backed by a circular resizable array. It has no synchronization locks (unlike legacy Stack) and creates zero node wrapper objects (unlike LinkedList), providing the fastest Stack and Queue performance in the JDK.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Legacy Stack Class →

Why ArrayDeque replaces Stack.

Java LinkedList →

Implementing Deque via linked nodes.

Java Iterators →

Queue traversal.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!