The stack consists of a different collection of frameworks that are used for storing different types of objects in Java. The stack is implemented using the stack keyword in Java. Certain operations are performed on the stack like push and pop. Apart from these operations, there are numerous methods that are applied on the Stack and iterator is one of the methods.
This Java post will illustrate several methods for iterating over a stack.
How to Iterate Over a Stack in Java?
The term “iteration” refers to fetching the elements from the declared Stack. Iteration is an important aspect that loops through the collections. The iteration consists of three different methods that are implemented using different packages of Java. The three different methods are listed below.
- The iterator() method
- For each() method
- The listiterator() method
Method 1: Iteration on a Stack Using the iterator() Method
This method belongs to the iterator interface. It is used to return the iterator over the elements from the stack. The syntax and the code of the iterator method are depicted below:
Iterator<T> iterator()
Here is a practical example that depicts the use of the iterator() method:
import java.util.Iterator;
import java.util.Stack;
//Declare a class for the iterator() method
class Iteratormethod
{
public static void main (String[] args)
{
Stack s = new Stack();
//Use the push() method to add the elements to the stack
System.out.println("The Implementation of iteration method");
s.push("Python");
s.push("C++");
s.push("Java");
s.push("Ruby");
//Use an iterator to iterate over the elements
Iterator ite = s.iterator();
while(ite.hasNext())
{
Object val = ite.next();
System.out.println(val);
}
}
}
In the above code:
- The required packages for the “ iterator” are imported.
- The next step involves declaring a class as “iteratormethod”.
- The elements are pushed with the help of the push() method.
- The iteration is done using the Java’s iterator() method of the Stack class
- The while loop iterates through each element on the stack.
- The println() method prints the Stack using the object “val”.
Output
The below output that depicts each element of the Stack is printed.
Method 2: forEach() Method
This method is described under the Stream and iterable interface of Java. It is used for iterating over the Stack in Java. The code below implements the forEach() method with the following syntax.
default void forEach(Consumer<super T>action)
Here is a practical demonstration of the forEach() method:
import java.util.*;
class foreachmethod
{
public static void main (String[] args)
{
Stack <Integer> t= new Stack<>();
//Add the elements using the push() method
t.push(158);
t.push(200);
t.push(1000);
t.push(100);
System.out.println("Stack iteration using forEach() Method:");
//invoking forEach() method for iteration over the stack
t.forEach(n -> {
System.out.println(n);
});
}
}
The push() method is used to add the elements to the stack and the forEach() is used to iterate over the Stack.
Output
The output below depicts that the forEach() method successfully iterated through the given Stack.
Method 3: Listiterator Method
The listiterator() method iterates the Stack from top to bottom and retrieves the elements from the last to the start. The syntax and the code are depicted below:
ListIterator listIterator(int index)
Here is how the Listiterator method works in Java:
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
//Declare class
class listiteratormethod
{
public static void main (String[] args)
{
Stack <String> i = new Stack<>();
i.push("Java");
i.push("Programming");
i.push("Python");
i.push("Programming");
ListIterator<String> ListIterator = i.listIterator(i.size());
System.out.println("Stack iteration from last to first:");
while (ListIterator.hasPrevious())
{
String a = ListIterator.previous();
System.out.println(a);
}
}
}
In the above Java code:
- The packages required for the listiterator() method are imported and the listIterator() method is applied to perform iteration.
Output
The below output prints the results from the end that is “Java” then “programming”.

Conclusion
To iterate over a stack in Java, first, an object is declared along with the type of the data either Integer or String. After that users can use the iterator, forEach, or Listiterator method to iterate through the Stack in Java. In this article, we have implemented different methods to iterate over a stack in Java.