Java Stacks and Queues
A stack is a data structure where the last item added is the first item removed. A queue is a data structure where the first item added is the first item removed.
Stacks and queues are simple ideas, but they appear in many algorithms. A stack is useful for undo history, parsing, and backtracking. A queue is useful for tasks waiting in order, breadth-first search, and scheduling work.
Stack: Last In, First Out
A stack follows LIFO: last in, first out. Imagine stacking plates. You put each new plate on top, and you usually remove the top plate first.
In Java, prefer ArrayDeque for stack behavior. The older Stack class still exists, but ArrayDeque is usually the better modern choice for single-threaded code.
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
Deque<String> undoStack = new ArrayDeque<>();
undoStack.push("Type title");
undoStack.push("Add paragraph");
undoStack.push("Insert image");
System.out.println("Top action: " + undoStack.peek());
System.out.println("Undo: " + undoStack.pop());
System.out.println("Undo: " + undoStack.pop());
System.out.println("Remaining: " + undoStack);
}
}
Output:
Top action: Insert image
Undo: Insert image
Undo: Add paragraph
Remaining: [Type title]
The push() method adds an item to the top of the stack. The peek() method reads the top item without removing it. The pop() method removes and returns the top item.
Queue: First In, First Out
A queue follows FIFO: first in, first out. Imagine a line at a ticket counter. The person who arrived first is served first.
Use the Queue interface when you want to show that your code uses queue behavior. ArrayDeque is a common implementation.
import java.util.ArrayDeque;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> printQueue = new ArrayDeque<>();
printQueue.offer("Report.pdf");
printQueue.offer("Invoice.pdf");
printQueue.offer("Notes.txt");
System.out.println("Next: " + printQueue.peek());
System.out.println("Printing: " + printQueue.poll());
System.out.println("Printing: " + printQueue.poll());
System.out.println("Waiting: " + printQueue);
}
}
Output:
Next: Report.pdf
Printing: Report.pdf
Printing: Invoice.pdf
Waiting: [Notes.txt]
The offer() method adds an item to the back of the queue. The peek() method reads the front item without removing it. The poll() method removes and returns the front item.
Common Operations
| Structure | Add | Read next | Remove next | Order |
|---|---|---|---|---|
| Stack | push(value) |
peek() |
pop() |
Last in, first out |
| Queue | offer(value) |
peek() |
poll() |
First in, first out |
Handling Empty Structures
Calling peek() on an empty ArrayDeque returns null. Calling poll() on an empty queue also returns null. For stack-style removal, pop() throws an exception if the stack is empty, so check isEmpty() first when the stack might have no items.
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
Deque<Integer> numbers = new ArrayDeque<>();
numbers.push(10);
numbers.push(20);
while (!numbers.isEmpty()) {
System.out.println(numbers.pop());
}
System.out.println("Empty: " + numbers.isEmpty());
}
}
Output:
20
10
Empty: true
When To Use Each One
- Use a stack when the most recent item should be handled first.
- Use a queue when the oldest waiting item should be handled first.
- Use
ArrayDequefor most stack and queue examples in beginner Java programs. - Avoid adding
nullvalues to anArrayDeque; it does not allow them.
Takeaway: stacks process the newest item first, while queues process the oldest item first.
