Iterate through Stack in Java
This post will discuss various methods to iterate through a stack in Java.
Before we begin, we encourage you to read the following post that points out a bug in Stack
class that causes stack elements to be printed in FIFO order instead of the expected LILO order. For example, iterator()
method on java.util.Stack
iterates through a stack in a bottom-up manner.
Following is a simple Java program that covers all approaches to iterate through a stack in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import com.google.common.collect.FluentIterable; import com.google.common.collect.Iterables; import java.util.*; import java.util.stream.Stream; class Main { // Iterate through a stack in Java public static void main(String[] args) { // if we use a stack, the output will be [1, 2, 3] Stack<Integer> stack = new Stack<>(); // if we use deque, output will be [3, 2, 1] // Deque<Integer> stack = new ArrayDeque<>(); stack.push(1); stack.push(2); stack.push(3); /* Using an iterator */ // 1. Using an iterator to iterate through a stack Iterator<Integer> itr = stack.iterator(); // hasNext() returns true if the stack has more elements while (itr.hasNext()) { // next() returns the next element in the iteration System.out.println(itr.next()); } // 2. Using enhanced for-loop (uses Iterator internally) for (Integer item: stack) { System.out.println(item); } /* Java 8 and above */ // 3. Java 8 – get the stream and use a lambda expression stack.stream().forEach(S -> System.out.println(S)); // 4. Java 8 – get the stream and provide a method reference stack.stream().forEach(System.out::println); // 5. Java 8 – Stream.of() + toArray() + forEach() Stream.of(stack.toArray()).forEach(System.out::println); // 6. Java 8 – stack inherits `forEach()` from the `Iterable` interface stack.forEach(System.out::println); // 7. Java 8 – `iterator()` is inherited from the `Collection` interface stack.iterator().forEachRemaining(System.out::println); /* using Enumeration */ // 8. Convert the stack into a vector and use the `Enumeration` interface @Deprecated Enumeration<Integer> enumeration = new Vector(stack).elements(); while (enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } // 9. Collections.enumeration() returns an enumeration enumeration = Collections.enumeration(stack); while (enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } /* Converting Stack to string using `toString()` method */ // 10. Calling `toString()` on stack System.out.println(stack.toString()); // 11. Using `Stream.of()` Stream.of(stack.toString()).forEach(System.out::println); /* Converting Stack to array using `toArray()` method */ Integer[] arr = null; // 12. Use `Stack.toArray(Integer[])` that returns an `Integer[]` array arr = stack.toArray(new Integer[stack.size()]); System.out.println(Arrays.toString(arr)); // 13. Without allocating any memory (JVM will take care) arr = stack.toArray(new Integer[0]); System.out.println(Arrays.toString(arr)); // 14. Use `Stack.toArray()` that returns an `Object[]` array System.out.println(Arrays.toString(stack.toArray())); // 15. Java 8 – Streams + method references System.out.println(Arrays.toString( stack.stream().toArray(Integer[]::new))); // 16. Java 8 – Streams + lambda expressions arr = stack.stream().toArray(n -> new Integer[n]); System.out.println(Arrays.toString(arr)); // 17. Using `FluentIterable` class from Guava library arr = FluentIterable.from(stack).toArray(Integer.class); System.out.println(Arrays.toString(arr)); // 18. Using `Iterables` class from Guava library arr = Iterables.toArray(stack, Integer.class); System.out.println(Arrays.toString(arr)); } } |
That’s all about iterating through Stack in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)