Java Programming Handbook

    Understanding Queue Interface in Java

    The Queue interface in Java is a part of the Java Collection Framework (JCF) and is used to store elements in a FIFO (First-In-First-Out) order. This makes it suitable for scenarios like task scheduling, buffering, and managing requests in an orderly fashion.

    In this blog, we will explore the Queue interface, its key methods, and its implementations: PriorityQueue, ArrayDeque, and LinkedList (as a Queue).

    What is the Queue Interface?#

    The Queue interface is present in the java.util package and extends the Collection interface.

    public interface Queue<E> extends Collection<E> { // Queue-specific methods }

    Features of Queue#

    Queue in Java
    • FIFO Order: The first element added is the first to be removed.
    • Allows Duplicates: Unlike Set, a Queue can contain duplicate elements.
    • Dynamic Size: Automatically resizes based on the number of elements.
    • Specialized Methods: Includes methods for adding, removing, and inspecting elements.

    Key Methods of Queue Interface#

    MethodDescription
    offer(E e)Adds an element to the queue, returning false if it fails.
    add(E e)Adds an element to the queue, throwing an exception if it fails.
    poll()Retrieves and removes the head element, returning null if empty.
    remove()Retrieves and removes the head element, throwing an exception if empty.
    peek()Retrieves the head element without removing it, returning null if empty.
    element()Retrieves the head element without removing it, throwing an exception if empty.

    Implementations of Queue Interface

    1. PriorityQueue (Min Heap)#

    Characteristics:#

    • Uses a heap data structure internally.
    • Elements are ordered based on priority, not insertion order.
    • Does not allow null values.
    • Not synchronized (not thread-safe).

    Example:#

    import java.util.*; public class PriorityQueueExample { public static void main(String[] args) { Queue<Integer> pq = new PriorityQueue<>(); pq.offer(30); pq.offer(10); pq.offer(20); System.out.println("PriorityQueue: " + pq); System.out.println("Head element: " + pq.peek()); } }

    Output:

    PriorityQueue: [10, 30, 20] Head element: 10

    Note: The smallest element (10) is placed at the head because PriorityQueue uses a Min Heap.

    2. ArrayDeque (Double-Ended Queue)#

    Characteristics:#

    • Uses a resizable array internally.
    • Fast insertion & deletion from both ends.
    • Allows null values.
    • Not synchronized.

    Example:#

    import java.util.*; public class ArrayDequeExample { public static void main(String[] args) { Deque<String> deque = new ArrayDeque<>(); deque.offer("Apple"); deque.offer("Banana"); deque.offerFirst("Mango"); System.out.println("ArrayDeque: " + deque); } }

    Output:

    ArrayDeque: [Mango, Apple, Banana]

    3. LinkedList (As a Queue)#

    Characteristics:#

    • Implements both Queue and Deque interfaces.
    • Uses a doubly linked list internally.
    • Fast insertion & deletion but slower access.

    Example:#

    import java.util.*; public class LinkedListQueueExample { public static void main(String[] args) { Queue<Integer> queue = new LinkedList<>(); queue.offer(100); queue.offer(200); queue.offer(300); System.out.println("LinkedList Queue: " + queue); System.out.println("Removed Element: " + queue.poll()); System.out.println("Queue after poll: " + queue); } }

    Output:

    LinkedList Queue: [100, 200, 300] Removed Element: 100 Queue after poll: [200, 300]

    Choosing the Right Queue Implementation#

    FeaturePriorityQueueArrayDequeLinkedList (Queue)
    OrderingNatural order (Min Heap)FIFO / LIFOFIFO
    Internal StructureHeapDynamic ArrayDoubly Linked List
    Insert/Delete SpeedMediumFastFast
    Search SpeedSlowFastSlow
    Allows null values?NoYesYes
    Thread SafetyNoNoNo

    Conclusion#

    In this blog, we explored the Queue interface and its implementations: PriorityQueue, ArrayDeque, and LinkedList. Each implementation has its own advantages and use cases. If you need priority-based ordering, use PriorityQueue. For fast insertions/deletions at both ends, use ArrayDeque. For simple FIFO operations, LinkedList works well.

    Last updated on Apr 09, 2025