DSA in Java: Stacks & Queues Explained (Beginner Guide)
Learn stacks (LIFO) and queues (FIFO) in Java with clear analogies and code: push/pop, offer/poll, O(1) operations, and the balanced-brackets interview problem.
Stacks and queues are two of the simplest data structures you'll ever meet — and two of the most useful. They're really just rules for the order in which you add and remove items. Once you see them, you'll spot them everywhere: your browser's back button, an undo feature, a printer queue, even how recursion works under the hood. Let's make both crystal clear.
Stack: Last In, First Out (LIFO)
A stack is like a pile of plates: you add to the top, and you remove from the top. The last item you put on is the first one you take off — LIFO. You can only ever touch the top plate.
Java gives you Deque (via ArrayDeque) as the recommended stack. The three core operations are all O(1):
Deque<Integer> stack = new ArrayDeque<>(); stack.push(1); // add to top stack.push(2); stack.push(3); // stack (top→bottom): 3, 2, 1 int top = stack.peek(); // look at top (3) without removing int out = stack.pop(); // remove & return top → 3 // stack is now: 2, 1
Queue: First In, First Out (FIFO)
A queue is like a line at a shop: people join at the back and are served from the front. The first to arrive is the first served — FIFO. It's the fair, in-order structure.
Queue<String> queue = new LinkedList<>(); queue.offer("A"); // add to back queue.offer("B"); queue.offer("C"); // queue (front→back): A, B, C String front = queue.peek(); // look at front ("A") String out = queue.poll(); // remove & return front → "A" // queue is now: B, C
Stack vs Queue at a glance
| Stack | Queue | |
|---|---|---|
| Order | LIFO (last in, first out) | FIFO (first in, first out) |
| Add | push (top) |
offer (back) |
| Remove | pop (top) |
poll (front) |
| Analogy | Pile of plates | Queue at a shop |
All the core operations — push, pop, offer, poll, peek — are O(1) on both structures. That speed is exactly why they're used as building blocks inside bigger algorithms.
A classic problem: balanced brackets
A perfect showcase for a stack: check whether brackets in a string like "{[()]}" are correctly matched. Push each opening bracket; when you see a closing one, it must match whatever's on top:
boolean isBalanced(String s) { Deque<Character> stack = new ArrayDeque<>(); for (char c : s.toCharArray()) { if (c == '(' || c == '[' || c == '{') { stack.push(c); // opening → push } else { if (stack.isEmpty()) return false; // nothing to match char open = stack.pop(); if ((c==')' && open!='(') || (c==']' && open!='[') || (c=='}' && open!='{')) return false; // mismatched pair } } return stack.isEmpty(); // all matched? then empty at the end }
This exact problem (and its variations) is one of the most common interview questions there is — and the stack makes it almost trivial once you see the pattern.
What made stacks and queues finally stick for me was the everyday analogies: a stack is a pile of plates, a queue is a line at a shop. I stopped memorising "LIFO/FIFO" as abstract acronyms and just pictured the plates and the line — and suddenly it was obvious which structure a problem needed. When you're solving a problem and thinking "I need to process these in reverse order of arrival," that's a stack. "In the order they arrived"? That's a queue. Queues also become essential very soon, when you learn graph BFS — so this lesson quietly sets up bigger things.
Key takeaways
- A stack is LIFO (pile of plates):
push/pop/peekthe top — all O(1). UseArrayDeque. - A queue is FIFO (line at a shop):
offerat the back,pollfrom the front — all O(1). - Stacks power undo, the back button, bracket matching, and the recursion call stack.
- Queues power fair in-order processing and graph BFS (coming up).
← Previous: DSA Lesson 7 — Linked Lists
Next: DSA Lesson 9 — Hashing (HashMap & HashSet) →
↑ Back to the DSA roadmap