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()); // secondDeque 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 insteadBest 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 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.