C

Core Java tutorial for beginners

Clean • Professional

Stack in Java – LIFO, Methods, Implementation & Examples

2 minute

Stack in Java

In Java, Stack is a class inside the java.util package and is considered a legacy collection. Even though Java provides modern alternatives like ArrayDeque, the Stack class is still widely used for learning and interviews.

What is Stack in Java?

A Stack is a LIFO (Last-In, First-Out) data structure where elements are added and removed only from the top.

It is a legacy class in Java, extends Vector, and is synchronized.


Where Stack Fits in Java Collection Hierarchy

Iterable
   └── Collection
          └── List
                └── Vector
                      └── Stack

Stack also inherits:

  • Synchronization
  • Dynamic growing capability
  • RandomAccess, Cloneable, Serializable (via Vector)

Key Features of Stack

FeatureExplanation
LIFO StructureLast element added is the first removed
Thread-SafeInherited synchronization from Vector
Allows NullYes
Allows DuplicatesYes
Legacy ClassReplaced by ArrayDeque in modern Java
Dynamic SizeGrows like Vector

When to Use Stack

Use Stack when:

  • You need LIFO behavior
  • You want built-in synchronization
  • Working with older codebases

Avoid Stack when:

  • You want fast, modern LIFO structure → use ArrayDeque

How to Create a Stack in Java

import java.util.Stack;

public class Main {
    public static void main(String[] args) {

        Stack<String> stack = new Stack<>();

        stack.push("Java");
        stack.push("Python");
        stack.push("C++");

        System.out.println(stack);
    }
}

Output:

[Java, Python, C++]

Common Stack Methods

learn code with durgesh images

1. Add / Push Methods

push(E item)

Adds an element on top of the stack.

stack.push("HTML");

2. Get / Access Methods

peek()

Returns the top element without removing it.

stack.peek();

3. Remove Methods

pop()

Removes and returns the top element.

stack.pop();

remove(int index)

Removes element at specific index (inherited from Vector).

stack.remove(0);

4. Search Methods

search(Object o)

Returns 1-based position from top; returns -1 if not found.

stack.search("Java");

contains(Object o)

Checks if the element exists.

stack.contains("Python");

5. Utility Methods

empty()

Checks if the stack is empty.

stack.empty();

size()

Returns number of elements.

stack.size();

Time Complexity of Stack

OperationTime ComplexityExplanation
push()O(1)Insert at top
pop()O(1)Remove top
peek()O(1)Access top
search()O(n)Linear search
contains()O(n)Linear search

Stack vs ArrayDeque

FeatureStackArrayDeque
LegacyYesNo
SynchronizedYesNo
SpeedSlowerFaster
RecommendedNoYes
ImplementationVectorResizable array

Advantages of Stack

  • Simple LIFO operations
  • Thread-safe (via Vector)
  • Easy to use in multithreaded context

Disadvantages of Stack

  • Slow due to synchronization
  • Considered outdated
  • ArrayDeque is faster and modern
  • Inherits overhead from Vector

Real-World Use Cases of Stack

  • Expression evaluation (postfix/prefix)
  • Undo/Redo operations
  • Parsing (XML, HTML)
  • DFS (Depth-First Search)
  • Backtracking algorithms
  • Browser navigation (Back button)

Article 0 of 0