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.
| 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) |
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
}
}
}
ArrayDeque: High-Speed Stack & Queueimport 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
}
}
Now let us explore Iterators, ListIterator, and Fail-Fast vs Fail-Safe mechanics. Proceed to: Java Iterators →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Why ArrayDeque replaces Stack.
Implementing Deque via linked nodes.
Queue hierarchy.
Queue traversal.