15 ways to Iterate through Stack in Java

In this post, we will discuss various methods to iterate through Stack in Java.


 

Before we begin, we encourage you to read below post that points out a bug in Stack class that causes stack elements to be printed in FIFO order instead of expected LILO order. For example, iterator() method on java.util.Stack iterates through a stack in bottom-up manner.

Potential Bug in Stack Class and a Workaround

 
Below is a simple Java program that covers all approaches to iterate through Stack in Java –

Java

Download   Run Code

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz