Skip to main content

Lesson 5 of 10

Queue & Deque

Queue models FIFO (first-in, first-out) - like a ticket counter. Deque (double-ended queue) supports adding/removing from both ends - it's both a stack (LIFO) and a queue (FIFO). PriorityQueue processes elements in priority order rather than insertion order.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Queue, Deque, PriorityQueue

Queue, Deque, PriorityQueue
import java.util.*;

public class QueueDemo {
    public static void main(String[] args) {
        // ── ArrayDeque as Queue (FIFO) ──────────────
        Queue<String> waitlist = new ArrayDeque<>();
        waitlist.offer("Alice");  // add to tail (never throws)
        waitlist.offer("Bob");
        waitlist.offer("Charlie");

        System.out.println(waitlist.peek());  // "Alice" - look without removing
        System.out.println(waitlist.poll());  // "Alice" - remove from head; null if empty
        System.out.println(waitlist.poll());  // "Bob"
        System.out.println(waitlist.size());  // 1

        // ── ArrayDeque as Stack (LIFO) ──────────────
        Deque<String> stack = new ArrayDeque<>();
        stack.push("page1");  // push to top (head)
        stack.push("page2");  // push to top
        stack.push("page3");

        System.out.println(stack.peek());     // "page3" - top of stack
        System.out.println(stack.pop());      // "page3" - remove from top

        // Deque used as deque (both ends)
        Deque<Integer> deque = new ArrayDeque<>();
        deque.addFirst(2);   // [2]
        deque.addLast(3);    // [2, 3]
        deque.addFirst(1);   // [1, 2, 3]
        System.out.println(deque.removeFirst()); // 1 → [2, 3]
        System.out.println(deque.removeLast());  // 3 → [2]

        // ── PriorityQueue - min-heap by default ──────
        Queue<Integer> pq = new PriorityQueue<>();
        pq.offer(30);
        pq.offer(10);
        pq.offer(20);
        while (!pq.isEmpty()) {
            System.out.print(pq.poll() + " ");  // 10 20 30 (ascending)
        }

        // Custom priority - max-heap (largest first)
        Queue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());
        maxHeap.offer(30); maxHeap.offer(10); maxHeap.offer(20);
        while (!maxHeap.isEmpty()) {
            System.out.print(maxHeap.poll() + " ");  // 30 20 10

        // Custom: books by number of reservations (highest first)
        PriorityQueue<Book> highDemand = new PriorityQueue<>(
            Comparator.comparingInt(Book::getReservationCount).reversed()
        );
        }
    }
}

Quick Quiz

Tip

Tip

Practice Queue Deque in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Queue & Deque

Practice Task

Note

Practice Task - (1) Write a working example of Queue Deque from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Queue Deque is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/10 done