Codectionary / Developer documentation / Java

Queue & Deque

Queue is an interface representing a first-in-first-out (FIFO) collection, typically implemented by LinkedList or the more efficient ArrayDeque. Deque (double-ended queue) extends Queue and allows insertion and removal at both ends, making it suitable for both queue (FIFO) and stack (LIFO) behavior. Queue methods come in two flavors: ones that throw an exception on failure (add, remove) and safer ones that return a special value instead (offer, poll).

Syntax

Queue<Type> queue = new LinkedList<>();\nDeque<Type> deque = new ArrayDeque<>();

Examples

Basic Queue (FIFO)

First element added is the first one removed.

import java.util.LinkedList;
import java.util.Queue;

Queue<String> line = new LinkedList<>();
line.offer("Customer A");
line.offer("Customer B");
line.offer("Customer C");

System.out.println(line.poll());  // Customer A - first in, first out
System.out.println(line.peek());   // Customer B - look without removing
System.out.println(line);          // [Customer B, Customer C]

ArrayDeque as a Stack (LIFO)

ArrayDeque used for last-in-first-out behavior, generally preferred over the legacy Stack class.

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

Deque<String> stack = new ArrayDeque<>();
stack.push("first");
stack.push("second");
stack.push("third");

System.out.println(stack.pop());  // third - last in, first out
System.out.println(stack.pop());  // second

Deque Operations at Both Ends

Deque allows adding and removing from either the front or the back.

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

Deque<Integer> deque = new ArrayDeque<>();
deque.addFirst(2);
deque.addLast(3);
deque.addFirst(1);
deque.addLast(4);

System.out.println(deque);  // [1, 2, 3, 4]

System.out.println(deque.removeFirst());  // 1
System.out.println(deque.removeLast());    // 4
System.out.println(deque);                  // [2, 3]

safe (offer/poll) vs throwing (add/remove) Methods

Understanding the two method families and when to use each.

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

Queue<Integer> queue = new ArrayDeque<>();

queue.offer(1);  // returns false on failure, instead of throwing
boolean added = queue.offer(2);
System.out.println(added);  // true

Integer result = queue.poll();  // returns null if empty, instead of throwing
System.out.println(result);      // 1

// On an empty queue:
Queue<Integer> empty = new ArrayDeque<>();
System.out.println(empty.poll());  // null, no exception
// empty.remove();  // would throw NoSuchElementException instead

Best practices

  • Prefer ArrayDeque over the legacy Stack and Vector classes, and over LinkedList, for both stack and queue use cases - it is faster and more modern
  • Use offer()/poll()/peek() (the safe, non-throwing methods) in most cases, reserving add()/remove()/element() for situations where a failure genuinely indicates a bug
  • Use a Deque when you need flexibility to add/remove from both ends, and a plain Queue interface when you only need strict FIFO behavior
  • Check for null after poll()/peek() on an empty collection, since these methods return null rather than throwing in that case

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

ArrayList
ArrayList is a resizable array implementation of the List interface, part of java.util. Unlike a plain array, an ArrayList automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and iterating. Since generics require object types, an ArrayList of primitives (like int) actually stores their wrapper class (Integer) via autoboxing.
LinkedList
LinkedList is a doubly-linked list implementation of both the List and Deque interfaces. Unlike ArrayList, it stores elements as individual nodes linked to their neighbors, which makes inserting and removing elements at the beginning or middle much faster, at the cost of slower random access by index. Because it implements Deque, LinkedList can also be used directly as a stack or queue.
HashMap
HashMap stores data as key-value pairs, offering constant-time (average case) lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key overwrites the previous value. HashMap does not guarantee any particular ordering of its entries, unlike LinkedHashMap (which preserves insertion order) or TreeMap (which keeps keys sorted).
HashSet
HashSet is a collection that stores unique elements with no guaranteed ordering, backed by a HashMap internally. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides constant-time (average case) performance for adding, removing, and checking membership, making it ideal for deduplication and fast lookups.