Java — Stack Class | Code Factory

package com.example.java.programming.stack;import java.util.Stack;public class Test {
public static void main(String[] args) {
// Creating a Stack
Stack<String> stack = new Stack<>();
// Pushing new items to the Stack
stack.push("Narendra");
stack.push("Amit");
stack.push("Yogi");
stack.push("Thakur");
System.out.println("Stack => " + stack);
System.out.println();
// Popping items from the Stack
String top = stack.pop(); // Throws EmptyStackException if the stack is empty
System.out.println("Stack.pop() => " + top);
System.out.println("Current Stack => " + stack);
System.out.println();
// Get the item at the top of the stack without removing it
top = stack.peek();
System.out.println("Stack.peek() => " + top);
System.out.println("Current Stack => " + stack);
}
}
Stack => [Narendra, Amit, Yogi, Thakur]Stack.pop() => Thakur
Current Stack => [Narendra, Amit, Yogi]
Stack.peek() => Yogi
Current Stack => [Narendra, Amit, Yogi]
package com.example.java.programming.stack;import java.util.Stack;public class Test {
public static void main(String[] args) {
// Creating a Stack
Stack<String> stack = new Stack<>();
// Pushing new items to the Stack
stack.push("Narendra");
stack.push("Amit");
stack.push("Yogi");
stack.push("Thakur");
System.out.println("Stack : " + stack); // Check if the Stack is empty
System.out.println("Is Stack empty? : " + stack.isEmpty());
// Find the size of Stack
System.out.println("Size of Stack : " + stack.size());
// Search for an element
// The search() method returns the 1-based position of the element from the top of the stack
// It returns -1 if the element was not found in the stack
int position = stack.search("Yogi");
if (position != -1) {
System.out.println("Found the element \"Yogi\" at position : " + position);
} else {
System.out.println("Element not found");
}
}
}
Stack : [Narendra, Amit, Yogi, Thakur]
Is Stack empty? : false
Size of Stack : 4
Found the element "Yogi" at position : 2
package com.example.java.programming.stack;import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class Test {
public static void main(String[] args) {
// Creating a Stack
Stack<String> stackOfBooks = new Stack<>();
// Pushing new items to the Stack
stackOfBooks.push("Book 1");
stackOfBooks.push("Book 2");
stackOfBooks.push("Book 3");
stackOfBooks.push("Book 4");
System.out.println("Iterate over a Stack using Java 8 forEach() method :");
stackOfBooks.forEach(plate -> {
System.out.println(plate);
});
System.out.println("\nIterate over a Stack using iterator() :");
Iterator<String> platesIterator = stackOfBooks.iterator();
while (platesIterator.hasNext()) {
String plate = platesIterator.next();
System.out.println(plate);
}
System.out.println("\nIterate over a Stack using iterator() and Java 8 forEachRemaining() method :");
platesIterator = stackOfBooks.iterator();
platesIterator.forEachRemaining(plate -> {
System.out.println(plate);
});
System.out.println("\nIterate 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 = stackOfBooks.listIterator(stackOfBooks.size());
while (platesListIterator.hasPrevious()) {
String plate = platesListIterator.previous();
System.out.println(plate);
}
}
}
Iterate over a Stack using Java 8 forEach() method :
Book 1
Book 2
Book 3
Book 4
Iterate over a Stack using iterator() :
Book 1
Book 2
Book 3
Book 4
Iterate over a Stack using iterator() and Java 8 forEachRemaining() method :
Book 1
Book 2
Book 3
Book 4
Iterate over a Stack from TOP to BOTTOM using listIterator() :
Book 4
Book 3
Book 2
Book 1

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store