Is there a bug in java.util.Stack's Iterator?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Usually, no, there is not a bug in java.util.Stack’s iterator. The surprising behavior comes from the fact that Stack extends Vector, so its iterator follows Vector’s normal iteration order from index 0 upward. That means iteration is not a “pop order” traversal of the stack, which is why developers expecting top-to-bottom LIFO iteration often think something is wrong.
Why The Iterator Feels Wrong
A stack is conceptually LIFO: the last pushed element is the first popped. But Iterator on Stack does not mean “iterate in pop order.” It means “iterate the underlying collection in its normal order,” which for Vector is from the first element to the last.
This prints:
That is insertion-order iteration through the underlying Vector, not repeated popping from the top.
Why This Is By Design
Stack is an old class that inherits behavior from Vector. Its iterator is therefore inherited collection iteration behavior, not a special stack-specific traversal API.
So the design is arguably inconvenient for stack intuition, but it is not a bug in the sense of violating the implemented contract. The iterator still visits each element once in the collection’s standard iteration order.
Pop Order Requires A Different Approach
If you need LIFO traversal, use repeated pop() or choose a better data structure such as Deque.
This prints:
Now you are observing true stack behavior.
Why Deque Is Usually Better
Modern Java code usually prefers Deque implementations such as ArrayDeque for stack semantics.
With ArrayDeque, the API is a better fit for stack and queue operations, and you also avoid the legacy synchronization overhead and confusing Vector inheritance story of Stack.
Reverse Traversal Without Popping
If you need reverse-order viewing without mutating the stack, a list-based reverse traversal is more explicit than relying on the default iterator.
This keeps the stack intact while still walking from top to bottom.
The Real Design Lesson
The confusion around Stack is not that its iterator is broken. The real lesson is that Stack is a legacy type whose inheritance from Vector leaks into behavior that does not match modern stack expectations very well.
That is why many Java developers treat Deque as the better default stack abstraction.
Common Pitfalls
- Expecting
IteratoronStackto behave like repeatedpop(). - Assuming surprising iteration order means the implementation is buggy.
- Forgetting that
Stackinherits fromVectorand therefore inherits its iteration behavior. - Using
Stackin new code whenArrayDequewould be clearer and more modern. - Mutating the stack with
pop()when you really wanted read-only reverse traversal.
Summary
- '
java.util.Stackiteration follows inheritedVectororder, not LIFO pop order.' - That behavior is surprising for stack semantics but is not a bug in the normal contract sense.
- Use
pop()for actual stack consumption order. - Use reverse indexing or
Dequewhen you need top-to-bottom traversal semantics. - In modern Java,
ArrayDequeis usually a better stack choice thanStack.
Related reading
- Is there a built-in Binary Search Tree in .NET 4.0?
- Is there a common Java utility to break a list into batches?
- Is there a concurrent List in Java's JDK?
- Is there a data structure in C like a ConcurrentQueue which allows me to await an empty queue until an item is added?
- Is there a code sample for multiple producers in spring kafka?
- Is there a compatibility matrix of Spring-boot and Spring-cloud?
- Is there a difference between dfs and topological sort? Can topological ordering be achieved without using dfs?
- Is there a difference between using a dict literal and a dict constructor?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.