πŸŽ‰ Festival Deal: Save Big on Professional Courses with Real Projects – Apply Coupons FEST300OFF FEST500OFF
β†’ Click Here ←

Stack Legacy Class in Java  


Introduction
  • Stack is a legacy class in Java that extends the Vector class.
    • It is known as a legacy class because it was introduced in JDK 1.0 and later modified and integrated into the Java Collections Framework.
    • It is primarily retained for legacy code compatibility and is therefore used very infrequently in modern Java applications.
  • It is present in the java.util package.
  • It was introduced in JDK 1.0 as part of the original Collection classes.
  • Hierarchy:
    • Stack Class Hierarchy in Java
  • Syntax :-
    package java.util;
    
    public class Stack<E> extends Vector<E>
    {
        // Constructors
        
        // Methods
        
        // Fields
    }
    • Stack extends Vector class and inherits all its methods.
  • Stack is a LIFO (Last-In-First-Out) data structure where the last inserted element is the first to be removed.
    Stack Legacy Class in Java
Constructors of Stack Class
  • Below are the constructors defined in the Stack class:
Sr. No. Constructor Description
1 Stack() Constructs an empty Stack with an initial capacity of 10 (inherited from Vector).
Methods of Stack Class
  • Below are the methods defined specifically in the Stack class:
Sr. No. Method Description
1 E push(E item) Pushes (inserts) an element onto the top of the Stack and returns the element pushed.
2 E pop() Removes and returns the element at the top of the Stack. Throws EmptyStackException if the stack is empty.
3 E peek() Returns the element at the top of the Stack without removing it.
4 boolean empty() Tests whether the Stack is empty and returns true if no elements are present.
5 int search(Object o) Searches for the specified element in the Stack and returns its 1-based position from the top. Returns -1 if not found.
Program :
  • In the below program, we are directly using the Stack class from the java.util package.
  • import java.util.Stack;
    
    public class StackDemo
    {
        public static void main(String[] args)
        {
            Stack<String> stack = new Stack<>();
    
            // Pushing elements
            stack.push("Apple");
            stack.push("Banana");
            stack.push("Mango");
            stack.push("Banana"); // duplicate allowed
    
            System.out.println(stack);
    
            System.out.println("-------------------------");
    
            // Accessing top element
            System.out.println("Top Element (peek): " + stack.peek());
    
            System.out.println("-------------------------");
    
            // Popping element
            System.out.println("Popped Element: " + stack.pop());
            System.out.println(stack);
    
            System.out.println("-------------------------");
    
            // Searching element
            System.out.println("Position of 'Apple': " + stack.search("Apple")); // top is position 1
    
            System.out.println("-------------------------");
    
            // Checking if stack is empty
            System.out.println("Is Stack Empty? " + stack.isEmpty());
    
            System.out.println("-------------------------");
    
            // Iterating Stack
            for(String fruit : stack)
            {
                System.out.println(fruit);
            }
        }
    }
Properties of Stack Class:
  1. Stack is an index-based data structure, meaning the first element is inserted at index 0.
  2. Stack can store heterogeneous elements if generics are not used.
  3. Stack allows duplicate elements.
  4. Stack allows null values.
  5. Stack maintains insertion order, but elements are accessed in LIFO (Last In First Out) manner.
  6. Stack does not follow sorting order; sorting must be done explicitly if needed.
  7. Stack extends the Vector class, so it is synchronized and thread-safe.
  8. Stack guarantees for data consistency.