BackendBench
Coffee

Unravel › Java concepts › Collections and streams

Java language

Legacy collections: Vector, Hashtable, Enumeration

Vector, Hashtable, Stack, Enumeration and StringTokenizer date from Java 1.0.

Learn these first

At a glance

Mental model

Old tools in a garage: they still do the job, but each has a modern replacement with a better handle, and some are slower because they lock everything.

What happens, in order
  1. 1Vector is a thread-safe ArrayList (each method synchronized); Hashtable is a synchronized HashMap that forbids null keys and valuesStack extends Vector.
  2. 2Enumeration is the old iterator: while (e.hasMoreElements()) { e.nextElement(); }Convert with Collections.list(enumeration) or enumeration.asIterator().
  3. 3StringTokenizer splits text by delimiters: while (t.hasMoreTokens()) t.nextToken()It is discouraged: use String.split or Scanner.
  4. 4Servlets, Properties (a Hashtable), javax.naming and some JDBC and mail APIs still return these types
Remember
Watch out
Using Hashtable for thread safety and then doing check-then-act on itStoring null in a HashtableUsing StringTokenizer where empty fields matter (it skips them)
Example (highlighted lines explain it)
import java.util.*;public class Old {  static void dump(Hashtable<String, Integer> table) {    Enumeration<String> keys = table.keys();    while (keys.hasMoreElements()) {      String k = keys.nextElement();      System.out.println(k + "=" + table.get(k));    }  }  static int words(String s) {    StringTokenizer t = new StringTokenizer(s, " ,");    int n = 0;    while (t.hasMoreTokens()) { t.nextToken(); n++; }    return n;  }  static Vector<String> names() { return new Vector<>(); }}
  1. line 3 uses Hashtable, the core of Legacy collections: Vector, Hashtable, Enumeration
  2. line 4 uses Enumeration, the core of Legacy collections: Vector, Hashtable, Enumeration
  3. line 5 uses hasMoreElements, the core of Legacy collections: Vector, Hashtable, Enumeration
  4. line 6 uses nextElement, the core of Legacy collections: Vector, Hashtable, Enumeration
  5. line 11 uses StringTokenizer, the core of Legacy collections: Vector, Hashtable, Enumeration
  6. line 13 uses hasMoreTokens, the core of Legacy collections: Vector, Hashtable, Enumeration
Read the full explanation (about 1 min)

Vector, Hashtable, Stack, Enumeration and StringTokenizer date from Java 1.0. They still work and appear in old code, but modern equivalents are ArrayList, HashMap or ConcurrentHashMap, Iterator and String.split.

A mental model

Old tools in a garage: they still do the job, but each has a modern replacement with a better handle, and some are slower because they lock everything.

How it works

Vector is a thread-safe ArrayList (each method synchronized); Hashtable is a synchronized HashMap that forbids null keys and values. Stack extends Vector.

Enumeration is the old iterator: while (e.hasMoreElements()) { e.nextElement(); }. Convert with Collections.list(enumeration) or enumeration.asIterator().

StringTokenizer splits text by delimiters: while (t.hasMoreTokens()) t.nextToken(). It is discouraged: use String.split or Scanner.

Servlets, Properties (a Hashtable), javax.naming and some JDBC and mail APIs still return these types.

Going deeper

Synchronized methods do not make compound operations atomic, and they add lock cost to single-threaded code. Use ConcurrentHashMap, CopyOnWriteArrayList or an explicit lock where you need thread safety.

Replace them when you touch the code, but there is no need for a risky mass rewrite: behaviour differences (null handling, iteration order, fail-fast) can show up.

Under the hood

Vector grows by doubling its array (or by a fixed increment) and synchronizes on this; Hashtable uses a prime-sized table and synchronizes each method, which makes it slower than HashMap under contention.

Common mistakes

  • Using Hashtable for thread safety and then doing check-then-act on it.
  • Storing null in a Hashtable.
  • Using StringTokenizer where empty fields matter (it skips them).

How Unravel recognises it

When you paste code, Unravel looks for these signs of legacy collections: vector, hashtable, enumeration. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Legacy collections: Vector, Hashtable, Enumeration?

Vector, Hashtable, Stack, Enumeration and StringTokenizer date from Java 1.0. They still work and appear in old code, but modern equivalents are ArrayList, HashMap or ConcurrentHashMap, Iterator and String.split.

What are the most common mistakes with Legacy collections: Vector, Hashtable, Enumeration?

Using Hashtable for thread safety and then doing check-then-act on it. Storing null in a Hashtable. Using StringTokenizer where empty fields matter (it skips them).

How can I see Legacy collections: Vector, Hashtable, Enumeration in real code?

Paste your own Java into Unravel and it will point out where legacy collections: vector, hashtable, enumeration appears, explain how it works at the detail level you choose, and draw the flow of the code around it.