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

Runtime & versions

Java Virtual Machine. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

Java Language Specification

Specifications & further reading