Java Queue and Deque

A Java Queue is a collection for items waiting to be processed, usually in first-in, first-out order. A Deque, pronounced “deck”, is a double-ended queue that can add, read, and remove items from both the front and the back.

Queue and Deque are interfaces in java.util. A common implementation is ArrayDeque, which is fast for most queue and deque work and does not allow null elements.

Use Queue For First-In, First-Out

A queue works like a line: the first item added is the first item removed. Use offer() to add an item, peek() to read the next item without removing it, and poll() to remove and return the next item.

import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<String> tickets = new ArrayDeque<>();

        tickets.offer("A101");
        tickets.offer("A102");
        tickets.offer("A103");

        System.out.println("Next: " + tickets.peek());
        System.out.println("Serving: " + tickets.poll());
        System.out.println("Serving: " + tickets.poll());
        System.out.println("Remaining: " + tickets);
    }
}

Output:

Next: A101
Serving: A101
Serving: A102
Remaining: [A103]

The call to peek() shows A101 but leaves it in the queue. Each call to poll() removes the front item, so the queue moves forward.

Use Deque From Both Ends

A Deque can act like a queue, but it also has front and back methods. Use offerFirst() and offerLast() to add items. Use pollFirst() and pollLast() to remove items.

import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
    public static void main(String[] args) {
        Deque<String> history = new ArrayDeque<>();

        history.offerLast("Open file");
        history.offerLast("Edit line");
        history.offerLast("Save file");
        history.offerFirst("Start editor");

        System.out.println(history);
        System.out.println("First: " + history.peekFirst());
        System.out.println("Last: " + history.peekLast());
        System.out.println("Undo: " + history.pollLast());
        System.out.println(history);
    }
}

Output:

[Start editor, Open file, Edit line, Save file]
First: Start editor
Last: Save file
Undo: Save file
[Start editor, Open file, Edit line]

This example uses the back of the deque like the latest action in a history list. Removing from the back with pollLast() is useful for undo-style behavior.

Queue And Deque Methods

Method Meaning
offer(value) Adds a value to the back of a queue.
peek() Reads the next value without removing it.
poll() Removes and returns the next value.
offerFirst(value) Adds a value to the front of a deque.
offerLast(value) Adds a value to the back of a deque.
pollFirst() Removes and returns the front value.
pollLast() Removes and returns the back value.

Empty Queues

When a queue or deque is empty, peek() and poll() return null. That makes them beginner-friendly for many programs because they avoid an exception when there is no next item.

import java.util.ArrayDeque;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue<String> messages = new ArrayDeque<>();

        System.out.println(messages.peek());
        System.out.println(messages.poll());
        System.out.println(messages.isEmpty());
    }
}

Output:

null
null
true

There are also methods such as add(), remove(), and element(). Those can throw exceptions in some situations, so offer(), poll(), and peek() are often easier when learning.

Choosing Queue Or Deque

  • Use Queue when items should be processed in the order they arrive.
  • Use Deque when your program needs to work with both the front and the back.
  • Use ArrayDeque for most general queue and deque tasks.
  • Use LinkedList only when you specifically need its list behavior too.

Takeaway: use Queue for line-like processing and Deque when both ends of the collection matter.