Queues, Deques, and Priority Queues
Queues represent ordered work. ArrayDeque is a strong general-purpose implementation for stack and queue operations. PriorityQueue orders elements according to priority.
Deque<String> stack = new ArrayDeque<>();
stack.push("first");
stack.push("second");
System.out.println(stack.pop());
Queue<String> queue = new ArrayDeque<>();
queue.offer("job-1");
queue.offer("job-2");
System.out.println(queue.poll());
PriorityQueue<Integer> priorities = new PriorityQueue<>();
priorities.offer(50);
priorities.offer(10);
System.out.println(priorities.poll());
A priority queue's internal array is not globally sorted; it maintains a heap property sufficient to retrieve the highest- or lowest-priority element efficiently.
Key Takeaways
ArrayDequecan serve as both stack and queue.PriorityQueueexposes priority order through removal/peek, not full iteration order.- Use queue APIs such as
offerandpollfor clear semantics.