Working With Stack Class of Collection in Java

Introduction

 
In this article, we are going to describe all the methods and constructors of the Stack class. Java provides the easiest way to handle the Data Structure because it provides a direct class for common use. Stack is very powerfull data structure that is commonly used in the implementation of most of the algorithms.
 

What is Stack?

 
stackImage.png 
 
The Stack represents a last in, first out (LIFO) data structure and linear data structure. A stack has two fundamental operations, called push and pop. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into the underflow state (which means no items are present in stack to be removed).
 

Stack Class in Java

 
Stack is a subclass of Vector that implements a standard last-in, first-out stack. The Stack class extends the Vector class with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.
 
Note : when a stack is first initialized (created) than it has no elements.
 
Constructor Details
 
This class has only a default constructor; no other constructor.
 
public Stack() - Creates an empty Stack.
 
Method Details 
 
The Stack class has five methods.
 
1. push 
 
This method is used to insert an item into the stack. It pushes an item onto the top of this stack. This has exactly the same effect as the addElement(item) method of the Vector class. It takes one argument as the item. The item is the object that you want to insert.
  1. public Object push(Object item)   
2. pop
 
This method is used to retrieve an element from a stack. It removes the object at the top of this stack and returns that object as the value of this function. It throws the EmptyStackException exception if the stack is empty.
  1. public Object pop()   
3. peek
 
This method is used to retrieve only the top element of the Stack. This method does not remove the top element; it only accesses elements. It throws the EmptyStackException exception if the stack is empty.
  1. public Object peek()  
4. empty
 
This method is used to check whether the stack is empty or not. It's a Boolean method, so it returns true when the stack is empty (no item(s) in the stack) else it return false.
  1. public boolean empty()  
5. search
 
Returns the 1-based position where an object is on this stack. If the object o occurs as an item in this stack, this method returns the distance from the top of the stack of the occurrence nearest the top of the stack; the topmost item on the stack is considered to be at distance 1. The equals method is used to compare o to the items in this stack.
  1. public int search(Object o)  
Example
  1. public class StringOperation  
  2. {  
  3.     public static void main(String arg[])  
  4.     {  
  5.         Stringimport java.io.*;  
  6.         import java.util.*;  
  7.         public class StackExample {  
  8.             Stack < Integer > stack;  
  9.             String str;  
  10.             int num, n;  
  11.             public static void main(String[] args) {  
  12.                 StackExample q = new StackExample();  
  13.             }  
  14.             public StackExample() {  
  15.                 try {  
  16.                     stack = new Stack < Integer > ();  
  17.                     InputStreamReader ir = new InputStreamReader(System.in);  
  18.                     BufferedReader bf = new BufferedReader(ir);  
  19.                     System.out.print("Enter number of elements : ");  
  20.                     str = bf.readLine();  
  21.                     num = Integer.parseInt(str);  
  22.                     for (int i = 1; i <= num; i++) {  
  23.                         System.out.print("Enter elements : ");  
  24.                         str = bf.readLine();  
  25.                         n = Integer.parseInt(str);  
  26.                         stack.push(n);  
  27.                     }  
  28.                 } catch (IOException e) {}  
  29.                 System.out.print("Retrieved elements from the stack : ");  
  30.                 while (!stack.empty()) {  
  31.                     System.out.print(stack.pop() + "  ");  
  32.                 }  
  33.             }  
  34.         }  
  35.     }  
  36. }  
OUTPUT
 
 cmd output Stack.jpg