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.
Vector ClassVector is identical to ArrayList in functionality, with two crucial architectural differences:
Vector is declared with the synchronized keyword, imposing heavy locking penalties in single-threaded environments.Vector doubles in size (100% growth / 2.0x), consuming significantly more RAM than ArrayList (50% growth / 1.5x).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());
}
}
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.
Now let us explore the Set interface, hashing algorithms, and HashSet. Proceed to: Java HashSet & LinkedHashSet →
Accelerate your Java mastery by studying these tightly connected core tutorials:
The modern replacement for Stack.
The modern replacement for Vector.
Why method-level locks hurt speed.
JCF architecture.