Iterating over a Stack Example

Iterating over a Stack Example

This example shows various ways of iterating over a Stack.
  • Iterate over a Stack using Java 8 forEach().
  • Iterate over a Stack using iterator().
  • Iterate over a Stack using iterator() and Java 8 forEachRemaining() method.
  • Iterate over a Stack from Top to Bottom using listIterator().
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;

public class IterateOverStackExample {
    public static void main(String[] args) {
        Stack<String> stackOfPlates = new Stack<>();

        stackOfPlates.add("Plate 1");
        stackOfPlates.add("Plate 2");
        stackOfPlates.add("Plate 3");
        stackOfPlates.add("Plate 4");

        System.out.println("=== Iterate over a Stack using Java 8 forEach() method ===");
        stackOfPlates.forEach(plate -> {
            System.out.println(plate);
        });

        System.out.println("\n=== Iterate over a Stack using iterator() ===");
        Iterator<String> platesIterator = stackOfPlates.iterator();
        while (platesIterator.hasNext()) {
            String plate = platesIterator.next();
            System.out.println(plate);
        }

        System.out.println("\n=== Iterate over a Stack using iterator() and Java 8 forEachRemaining() method ===");
        platesIterator = stackOfPlates.iterator();
        while (platesIterator.hasNext()) {
            String plate = platesIterator.next();
            System.out.println(plate);
        }


        System.out.println("\n=== Iterate over a Stack from TOP to BOTTOM using listIterator() ===");
        // ListIterator allows you to traverse in both forward and backward directions.
        // We'll start from the top of the stack and traverse backwards.
        ListIterator<String> platesListIterator = stackOfPlates.listIterator(stackOfPlates.size());
        while (platesListIterator.hasPrevious()) {
            String plate = platesListIterator.previous();
            System.out.println(plate);
        }
    }
}
# Output
=== Iterate over a Stack using Java 8 forEach() method ===
Plate 1
Plate 2
Plate 3
Plate 4

=== Iterate over a Stack using iterator() ===
Plate 1
Plate 2
Plate 3
Plate 4

=== Iterate over a Stack using iterator() and Java 8 forEachRemaining() method ===
Plate 1
Plate 2
Plate 3
Plate 4

=== Iterate over a Stack from TOP to BOTTOM using listIterator() ===
Plate 4
Plate 3
Plate 2
Plate 1

Comments