Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

Stack

Stack is a popular data structure that operates on Last-In-First-Out (LIFO) principles. In order to work with the Stack data structure, we have a class in the java.util package, which is the java.util.Stack class.

java.util.Stack class :

public class java.util.Stack<E> extends java.util.Vector<E> {
public java.util.Stack();
public E push(E);
public synchronized E pop();
public synchronized E peek();
public boolean empty();
public synchronized int search(java.lang.Object);
}

As we can see above, java.util.Stack class is extending java.util.Vector class, which means java.util.Stack has all the functionality of java.util.Vector class.

            
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class JTC {
   public static void main(String[] args) {
      Stack stack = new Stack();
      stack.add(101);
      stack.add(102);
      stack.add(103);
      stack.add("Hello");
      stack.add("JTC");
      stack.add(true);
      stack.add(10.23);
      stack.add(null);
      System.out.print("Using Iterator :- ");
      Iterator iterator = stack.iterator();
      while (iterator.hasNext()) {
         System.out.print(iterator.next() + " ");
      }
      ListIterator listIterator = stack.listIterator();
      System.out.print("\n Using List Iterator (forward) :- ");
      while (listIterator.hasNext()) {
         System.out.print(listIterator.next() + " ");
      }
      System.out.print("\n Using List Iterator (reverse) :- ");
      while (listIterator.hasPrevious()) {
         System.out.print(listIterator.previous() + " ");
      }
   }
}
    Iterator :- 101 102 103 104 105 
    forward :- 101 102 103 104 105 
    reverse :- 105 104 103 102 101 
    Enumeration :- 101 102 103 104 105  
            

In this example, as we can see, we are creating a Stack or an object of the java.util.Stack class by using its default constructor and adding elements using the add() method of the java.util.Vector class, the same as Vector.
It the next part of the program we are using Iterator, List-Iterator and Enumeration to traverse Stack.

Important Methods of java.util.Vector class:

1. public E push(E): It pushes the elements to the top of the Stack.

2. public synchronized E pop() : It removes the object from the top of the Stack and returns it to the caller.

img required
            
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class JTC {
   public static void main(String[] args) {
      Stack stack = new Stack();
      stack.push(101);
      stack.push("Hello");
      stack.push(true);
      stack.push('A');
      stack.push(12.34);
      stack.push(-89.78 f);
      System.out.println(stack.pop());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
      System.out.println("stack :- " + stack);
      System.out.println(stack.pop());
   }
}
        
    -89.78
    12.34
    A
    true
    Hello
    101
    stack :- []
    Exception in thread "main" java.util.EmptyStackException
    at java.base/java.util.Stack.peek(Stack.java:101)
    at java.base/java.util.Stack.pop(Stack.java:83)
    at com.P1.JTC.main(JTC.java:26)
            

3. public synchronized E peek(): It peeks at the object from the top of the stack but doesn’t remove the element from the top of the stack, unlike the pop() method. If the stack is empty, then we get a java.util.EmptyStackException.

            
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class JTC {
   public static void main(String[] args) {
      Stack stack = new Stack();
      stack.push(101);
      stack.push("Hello");
      stack.push(true);
      stack.push('A');
      stack.push(12.34);
      stack.push(-89.78 f);
      System.out.println(stack.peek());
      System.out.println(stack.peek());
      System.out.println(stack.peek());
      System.out.println(stack.peek());
      System.out.println(stack.peek());
      System.out.println(stack.peek());
      System.out.println("stack :- " + stack);
   }
}
        
    -89.78
    -89.78
    -89.78
    -89.78
    -89.78
    -89.78
    stack :- [101, Hello, true, A, 12.34, -89.78]
            

4. public boolean empty(): It is basically used to check if the current working stack is empty. If the stack is empty, it returns true; otherwise, it returns false.

            
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class JTC {
   public static void main(String[] args) {
      Stack stack = new Stack();
      stack.push(101);
      stack.push("Hello");
      stack.push(true);
      stack.push('A');
      stack.push(12.34);
      stack.push(-89.78 f);
      System.out.println(stack.empty());
      stack.clear();
      System.out.println(stack.empty());
   }
}
    false
    true
            

5. public synchronized int search(java.lang.Object):- It searches for the element in the current working stack. If the element is found, it returns the distance of the element from the top of the stack. If the specified element is not in the stack, it returns -1.

            
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Stack;
public class JTC {
   public static void main(String[] args) {
      Stack stack = new Stack();
      stack.push(101);
      stack.push("Hello");
      stack.push(true);
      stack.push('A');
      stack.push(12.34);
      stack.push(-89.78 f);
      System.out.println(stack.search(101)); // 6 [distance is 6 from the top of the stack]
      System.out.println(stack.search("Hello")); // 5
      System.out.println(stack.search("JTC")); // -1 [because "JTC" is not is stack.]
   }
}
    6
    5
    -1