Collections Framework
Queues, Deques & Priority Queues
Queue offers FIFO with polite failure modes; Deque adds both ends (and replaces the legacy Stack); PriorityQueue always surrenders the smallest element. ArrayDeque is the right default for both stack and queue.- Two method families: throwing (
add/remove/element) vs value-returning (offer/poll/peek) ArrayDeque: circular array, faster thanLinkedListas a queue and thanStackas a stackPriorityQueue: binary heap;pollis O(log n); iteration is not sorted- Blocking variants (
ArrayBlockingQueue,LinkedBlockingQueue) live in Concurrent Collections
Deque<Task> queue = new ArrayDeque<>();
queue.offer(task); // enqueue at tail
Task next = queue.poll(); // dequeue from head (null if empty)
Deque<Frame> stack = new ArrayDeque<>();
stack.push(frame); // addFirst
Frame top = stack.pop(); // removeFirst (throws if empty)Queue<Job> jobs = new PriorityQueue<>(Comparator.comparing(Job::deadline));
jobs.addAll(pending);
while (!jobs.isEmpty()) {
process(jobs.poll()); // always the earliest deadline
}