Java Queue: Interface, Implementations, and Usage
Understand the Java Queue interface, its core implementations, performance tradeoffs, and concurrency behavior for practical developer use.

The java.util.Queue interface is a core part of the Java Collections Framework. It represents a collection designed to hold elements prior to processing, and it defines operations for insertion, removal, and examination. The interface itself does not mandate a specific ordering, but most common implementations use FIFO (first-in, first-out) order. To use a queue effectively, you need to understand the operation contract, the ordering and null-handling behavior of each implementation, and the thread-safety options.
The Queue Interface and Its Contract
The Queue interface specifies six queue operations in two styles: methods that throw an exception when they fail, and methods that return a special value (null or false). The table below summarizes them:
| Operation | Throws Exception | Returns Special Value |
|---|---|---|
| Insert | add(e) | offer(e) |
| Remove | remove() | poll() |
| Examine | element() | peek() |
For insertion, add(e) throws IllegalStateException if the queue cannot accept the element (for example, because capacity is full); offer(e) returns false instead. For removal, remove() throws NoSuchElementException if the queue is empty; poll() returns null. For examination, element() throws NoSuchElementException if the queue is empty; peek() returns null. The return-value variants are useful when queue-empty or queue-full conditions are expected in normal operation.
The Queue interface also inherits Collection methods like size(), isEmpty(), and contains(), but the six operations above are the ones you will use most often.
Core Implementations and Their Characteristics
Java provides several implementations of Queue, each with different ordering, null-handling, and thread-safety characteristics. The most commonly used are:
- LinkedList – A doubly-linked list that implements both
ListandDeque. It allowsnullelements and is not thread-safe. - ArrayDeque – A resizable-array implementation of
Deque. It does not allownullelements and is not thread-safe. - PriorityQueue – An unbounded priority queue based on a heap. It orders elements according to their natural ordering or a provided
Comparator. It does not allownull. - BlockingQueue implementations (e.g.,
ArrayBlockingQueue,LinkedBlockingQueue) – Designed for concurrent producer-consumer scenarios. They support blocking operations likeput()andtake().
The following table compares these implementations across key dimensions:
| Implementation | Ordering | Nulls Allowed | Thread-Safe | Typical Use Case |
|---|---|---|---|---|
LinkedList | FIFO | Yes | No | General-purpose queue; also implements List |
ArrayDeque | FIFO (when used as queue) | No | No | High-performance queue/deque when nulls are not needed |
PriorityQueue | Priority order | No | No | Processing elements by priority |
ArrayBlockingQueue | FIFO | No | Yes | Bounded producer-consumer |
LinkedBlockingQueue | FIFO | No | Yes | Unbounded or bounded producer-consumer |
Note that ArrayDeque and PriorityQueue do not allow null elements. If you need to store null, LinkedList is the standard non-concurrent Queue implementation that permits it. Blocking queues like ArrayBlockingQueue and LinkedBlockingQueue also reject null.
Choosing Between LinkedList and ArrayDeque
When you need a simple FIFO queue and do not require null elements, ArrayDeque is usually a better choice than LinkedList. It uses a resizable array internally, which offers better cache locality and lower per-element memory overhead than a doubly-linked list. Every LinkedList node stores two references plus the element, while ArrayDeque stores elements directly in a contiguous array.
For example, consider a typical queue usage:
Queue<String> queue = new ArrayDeque<>();
queue.offer("first");
queue.offer("second");
String head = queue.poll(); // "first"If you need null values, or you need both queue semantics and List operations, LinkedList is the standard implementation that supports both. Keep in mind that indexed access on LinkedList is O(n), so it is not a good fit when random access is the primary requirement.
PriorityQueue: When Ordering Matters
PriorityQueue is not FIFO; it orders elements according to their natural ordering or a custom Comparator. The head of the queue is the least element according to the specified ordering. This makes it useful for task scheduling, Dijkstra's algorithm, or any scenario where you need to process items by priority.
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(5);
pq.offer(1);
pq.offer(3);
int first = pq.poll(); // 1The time complexity for offer and poll is O(log n) because of the underlying heap. This is slower than the amortized O(1) time for ArrayDeque, but it is the cost of maintaining order. PriorityQueue is not thread-safe; if multiple threads access it concurrently, you must synchronize externally or use a PriorityBlockingQueue.
When using PriorityQueue, be aware that its iterator does not guarantee any particular order. If you need to traverse elements in priority order, you must repeatedly call poll().
BlockingQueue for Producer-Consumer Scenarios
For concurrent applications, the BlockingQueue interface extends Queue and adds blocking operations. The most important are put(e) and take(): put(e) inserts the element, waiting if necessary for space to become available, and take() retrieves and removes the head, waiting if necessary for an element to appear. These methods are ideal for producer-consumer patterns where producers and consumers run in separate threads.
BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
// Producer thread
queue.put("job");
// Consumer thread
String job = queue.take();put and take throw InterruptedException, so the calling thread must handle it.
ArrayBlockingQueue is a bounded, FIFO queue backed by an array. LinkedBlockingQueue is optionally bounded and uses linked nodes. Both are thread-safe and do not allow null elements. When using a bounded queue, put can block indefinitely, so consider offer(e, timeout, unit) to wait only for a limited time instead.
For unbounded queues, put never blocks, but the queue can grow without limit, which may lead to memory exhaustion. Always consider whether a bound is needed to protect against excessive memory usage.
Performance and Memory Considerations
Queue operation costs vary by implementation. ArrayDeque offers amortized O(1) time for offer, poll, peek, and size. LinkedList also offers O(1) time for offer and poll, but the constant factors are higher because each insertion allocates a node and operations follow pointers. PriorityQueue offers O(log n) time for offer and poll. Blocking queues add synchronization overhead; ArrayBlockingQueue uses a single lock and two conditions in the standard implementation.
Memory usage is another factor. ArrayDeque uses a contiguous array that may need to be resized when it grows; resizing copies elements and costs O(n), but the amortized cost per insertion is O(1). LinkedList allocates a new node for each element, so its fixed per-element overhead is higher. PriorityQueue also uses a dynamic array, and after growth its backing array can be larger than the number of elements it currently holds.
For most single-threaded queue needs, ArrayDeque is the best default. If you need priority ordering, use PriorityQueue. If you need thread safety, choose an appropriate BlockingQueue implementation.
Common Pitfalls and Edge Cases
One common mistake is calling add() on a bounded queue without handling the failure case. If the queue is full, add() throws IllegalStateException; use offer() and check its boolean result when you cannot guarantee capacity.
Another pitfall is calling remove() or element() on an empty queue. Those methods throw NoSuchElementException; poll() and peek() are often safer because they return null. However, if null is a valid element in your queue (only possible with LinkedList), a null result does not tell you whether the queue was empty. Use isEmpty() to distinguish those cases.
When using PriorityQueue, remember that its ordering should be consistent with equals. If you use a custom comparator, make sure it defines a consistent ordering; an inconsistent ordering can produce unpredictable behavior. Also, PriorityQueue rejects null elements with NullPointerException.
Finally, LinkedList, ArrayDeque, and PriorityQueue are not thread-safe. If multiple threads access the same queue instance, synchronize externally or use a BlockingQueue implementation. Concurrent unsynchronized access to these implementations will fail unpredictably.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.