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

Working of Stack in Java  


Steps of Working of Stack:
  • Below are the steps of working of Stack in Java:
    1. Creation of Stack:
      • A new Stack can be created using:
        Stack<Integer> stack = new Stack<>();
      • When created, the Stack is empty and its size is 0.
      • Internally, Stack is implemented as a subclass of Vector, which uses a dynamic array to store elements.
      • Example internal structure (initially empty):
        Top β†’ [ ]
    2. Pushing Elements onto the Stack:
      • Elements are added using the push() method:
        stack.push(10);
        stack.push(20);
        stack.push(30);
        stack.push(40);
        stack.push(50);
        stack.push(60);
      • Each new element is placed on the top of the stack (Last-In-First-Out principle).
      Push Elements in Stack Java
    3. Peeking at the Top Element:
      • The peek() method returns the top element without removing it.
        System.out.println(stack.peek()); // prints 60
      Peek in Stack Java
    4. Popping Elements from the Stack:
      • The pop() method removes and returns the top element of the stack.
        int top = stack.pop(); // Removes 60
      • After popping, the top shifts to the next element below it.
      Pop in Stack Java
    5. Searching Elements in the Stack:
      • The search() method returns position (1-based) of an element from the top of the stack.
        System.out.println(stack.search(20)); // returns 4 because 60 has been removed from stack
      • If the element is not found, it returns -1.
    6. Checking if Stack is Empty:
      • The empty() method checks whether the stack is empty.
        System.out.println(stack.empty()); // false
    7. Traversal of Stack:
      • Since Stack extends Vector, it can be traversed using Iterator or a for-each loop.
      • Example:
        for (Integer no : stack)
        {
            System.out.println(no);
        }
      • Elements will be printed from bottom to top order.

Stack is Good for:
  1. Implementing LIFO (Last-In-First-Out) operations:
    • Stack follows the LIFO principle β€” the last element added is the first one removed.
    • Suitable for use cases like undo/redo operations, expression evaluation, and function call management.
  2. Thread-safe operations:
    • Since Stack extends Vector, all its methods are synchronized.
    • Safe for multi-threaded environments where only one thread should modify the stack at a time.
  3. Simple and quick element management:
    • Provides predefined methods like push(), pop(), peek(), and search() for managing stack elements.
    • Easy to use for educational purposes or small stack-based operations.
  4. Legacy system support:
    • Used in older Java applications before the introduction of Deque and ArrayDeque.
    • Helpful when maintaining or upgrading legacy Java codebases.
Stack is Not Good for:
  1. Poor performance in single-threaded environments:
    • Synchronization adds unnecessary overhead when thread safety isn’t required.
    • ArrayDeque is much faster for single-threaded stack operations.
  2. Limited functionality:
    • The Stack class only supports basic stack operations (push, pop, peek, search).
    • Does not provide advanced concurrent features like ConcurrentLinkedDeque or BlockingDeque.
  3. Obsolete compared to modern classes:
    • Considered a legacy class and largely replaced by Deque or ArrayDeque since Java 1.5.
    • Modern alternatives are faster and more flexible.
  4. Sequential search performance:
    • The search() method performs a linear search (O(n)) starting from the top of the stack.
    • Inefficient for large stacks where random access is not supported.
Summary Table:
Operation Description Performance / Complexity
push(E item) Adds an element to the top of the stack. O(1)
pop() Removes and returns the top element. O(1)
peek() Returns the top element without removing it. O(1)
search(Object o) Searches the element from top to bottom (1-based index). O(n)
empty() Checks if the stack is empty. O(1)
Traversal Can use Iterator or for-each loop. O(n)
Thread Safety Yes (inherited synchronized methods from Vector). Overhead present