HOWTO · Java
Enqueue and Dequeue in Java
Learn how to enqueue and dequeue Java Queue elements, choose the right methods, and use LinkedList.
On this page
In Java, enqueueing adds an element to the tail of a FIFO queue, and dequeueing removes the element at its head. The Queue interface defines these operations; LinkedList is one class that implements it. Use offer and poll when you want return values to report whether insertion succeeded or no head element was available.
Queue and FIFO
A queue processes elements in first-in, first-out (FIFO) order: the first value added is the first value removed. Adding happens at the tail, while removal and inspection happen at the head. This ordering is useful when a program should process submitted jobs, events, or messages in arrival order.
Java’s Queue is an interface in the collections framework. It describes the usual queue operations without requiring a particular storage strategy. Implementations may have different capacity limits and performance characteristics, so choose one that fits the application rather than assuming every queue behaves alike.
For a related explanation of the ordering rule, see the Java FIFO queue guide.
Enqueue and Dequeue
Enqueue is the operation of adding an item at the tail. Dequeue is the operation of removing the item at the head. These names describe queue operations; they do not mean that the Deque interface is the same thing as a dequeue operation. Deque is a separate Java interface for collections that support insertion and removal at both ends.
A queue’s head can also be inspected without removing it. That is useful when code needs to decide what to process next while leaving the queue’s order intact.
Queue Methods in Java
For insertion, add(E) and offer(E) both add an element when the queue accepts it. Their capacity behavior differs: add throws IllegalStateException if a capacity restriction prevents insertion, while offer returns false. An unbounded queue may not impose a fixed capacity, so the result depends on the implementation.
For removal, remove() retrieves and removes the head, but throws NoSuchElementException if the queue is empty. poll() also retrieves and removes the head, but returns null when there is no element to remove. Use poll() when an empty queue is an expected state that the caller should handle through its return value.
element() and peek() inspect the head without removing it. element() throws NoSuchElementException when the queue is empty; peek() returns null. These paired methods let you choose between an explicit exception and a sentinel return value. Because poll() and peek() use null to report that no head value is available, avoid storing null in a queue when your logic depends on distinguishing that result from a stored value.
Implement Enqueue and Dequeue with LinkedList
The following complete program uses the Queue<String> interface with a LinkedList<String> implementation. It adds two values, checks the head, and then removes each value in FIFO order.
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.offer("First");
queue.offer("Second");
System.out.println("Head: " + queue.peek());
System.out.println("Removed: " + queue.poll());
System.out.println("Removed: " + queue.poll());
}
}
Head: First
Removed: First
Removed: Second
The recorded run produced the output shown above. The first inspection reads First without removing it, and the two calls to poll() then remove First followed by Second. The type is declared as Queue<String> so the code depends on the queue operations it needs, while LinkedList supplies the concrete implementation. Other Queue implementations can be substituted when their capacity and ordering behavior suit the application.
The key choice is the empty-queue and capacity policy your caller needs: use offer, poll, and peek when their return values are part of normal control flow, or use add, remove, and element when the corresponding exceptional condition should be surfaced to the caller.