Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

Vector and Stack

Reference for java.util.Vector and Stack: how these legacy synchronized classes work, why they are discouraged, and the modern replacements.

Vector<E> and Stack<E> come from Java 1.0, before the Collections Framework existed. Vector is a growable array like ArrayList, but every method is synchronized. Stack extends Vector and adds the classic push, pop and peek operations.

Both still work, and the old code you meet may use them. For new code they are discouraged: the per-method locking does not make compound operations safe and slows single-threaded code, and Stack inherits every List method, so it can be indexed and sorted, which defeats the idea of a stack. Use ArrayList instead of Vector, and ArrayDeque instead of Stack.

Java
Stack<Integer> stack = new Stack<>();
stack.push(1);
Deque<Integer> better = new ArrayDeque<>();

Try it

Java Vector and Stack basics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Why ArrayDeque is the better stack

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public class Stack<E> extends Vector<E>
Member Description
Vector() / Vector(int capacity) / Vector(int capacity, int increment) / Vector(Collection) Constructors. By default the capacity doubles when full.
addElement, elementAt, firstElement, lastElement, insertElementAt, removeElement, removeAllElements Legacy names that duplicate List methods.
E push(E item) (Stack) Puts the item on top and returns it.
E pop() (Stack) Removes and returns the top; throws EmptyStackException when empty.
E peek() (Stack) Returns the top without removing; throws EmptyStackException when empty.
boolean empty() (Stack) true if there are no elements.
int search(Object o) (Stack) Distance from the top (1 = top), or -1.

Usage notes

  • Synchronization is shallow: each call is atomic, but "check then act" sequences still need your own lock.
  • Order: Stack stores the top at the end of the vector, so printing shows the bottom first. Deque used as a stack shows the top first.
  • search() is 1-based from the top, unlike indexOf().
  • Replacements: ArrayList (or Collections.synchronizedList() / CopyOnWriteArrayList when sharing), ArrayDeque for stacks.
  • Enumeration (elements()) is the pre-1.2 iteration style; use iterators or for-each.
  • Still alive: some old APIs return Vector; the class is not deprecated formally.