Java Vector and Stack: Synchronized Legacy Classes & Modern Replacements

Vector and Stack are legacy collection classes introduced in JDK 1.0 prior to the invention of the unified Collections Framework in JDK 1.2. They were subsequently retrofitted to implement the List interface.

1. The Vector Class

Vector is identical to ArrayList in functionality, with two crucial architectural differences:

  • Synchronized: Almost every public method in Vector is declared with the synchronized keyword, imposing heavy locking penalties in single-threaded environments.
  • Growth Rate: When capacity is exceeded, Vector doubles in size (100% growth / 2.0x), consuming significantly more RAM than ArrayList (50% growth / 1.5x).

2. The Stack Class (LIFO)

Stack models a Last-In, First-Out (LIFO) container. Elements are pushed onto the top of the stack and popped from the top:

import java.util.Stack;

public class StackDemo {
    public static void main(String[] args) {
        Stack<String> browserHistory = new Stack<>();

        // 1. push(): Adds to top of stack
        browserHistory.push("https://veewom.com");
        browserHistory.push("https://veewom.com/java/");
        browserHistory.push("https://veewom.com/java/vector-stack.htm");

        // 2. peek(): Inspects top without removing
        System.out.println("Currently Viewing: " + browserHistory.peek());

        // 3. pop(): Removes and returns top item
        System.out.println("Clicked Back: " + browserHistory.pop());
        System.out.println("Now Viewing:   " + browserHistory.peek());
    }
}

Modern Best Practice Recommendation

Avoid using Vector and Stack in modern Java 21+ code! Replace Vector with ArrayList, and replace Stack with ArrayDeque (e.g. Deque<String> stack = new ArrayDeque<>();), which is significantly faster and enforces proper LIFO boundaries.

Next Step

Now let us explore the Set interface, hashing algorithms, and HashSet. Proceed to: Java HashSet & LinkedHashSet →

Frequently Asked Questions (FAQ)

Q1. What is the difference between Vector and ArrayList in Java?
Vector is synchronized (thread-safe); every method acquires a lock, imposing severe performance overhead. ArrayList is non-synchronized and dramatically faster. When resizing, Vector doubles its capacity (2.0x), whereas ArrayList grows by 50% (1.5x).
Q2. Why is the Stack class considered an obsolete design in modern Java?
Stack extends Vector, inheriting all vector index methods (like stack.add(index, element)), which violates the strict LIFO (Last-In-First-Out) abstraction of a true stack! Modern Java officially recommends using 'ArrayDeque' for stacks.
Q3. What are the core operations of the Stack class?
The primary methods are: push(E item) (pushes onto top of stack), pop() (removes and returns top item), peek() (inspects top item without removing), and empty() (checks if stack is empty).

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java ArrayDeque →

The modern replacement for Stack.

Java ArrayList →

The modern replacement for Vector.

Thread Synchronization →

Why method-level locks hurt speed.

Collections Overview →

JCF architecture.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!