Java — Stack Class | Code Factory

Code Factory
3 min readJun 23, 2020

Donate : Link

WordPress Blog : Link

A Stack is a Last In First Out (LIFO) data structure. It supports two basic operations called push and pop. The push operation adds an element at the top of the stack, and the pop operation removes an element from the top of the stack.

Java provides a Stack class which models the Stack data structure. The Stack class is part of Java’s collections framework. Following is the class hierarchy of Stack in Java -

The Stack class extends Vector which implements the List interface. A Vector is a re-sizable collection. It grows its size to accommodate new elements and shrinks the size when the elements are removed.

Since the Stack class extends Vector, it also grows and shrinks its size as needed when new elements are added or removed.

Push, Pop and Peek Operations :

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);
}
}

Output :

Stack => [Narendra, Amit, Yogi, Thakur]Stack.pop() => Thakur
Current Stack => [Narendra, Amit, Yogi]
Stack.peek() => Yogi
Current Stack => [Narendra, Amit, Yogi]

Example 2 :

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");
}
}
}

Output :

Stack : [Narendra, Amit, Yogi, Thakur]
Is Stack empty? : false
Size of Stack : 4
Found the element "Yogi" at position : 2

Example 3 :

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);
}
}
}

Output :

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

--

--