Unravel › Java concepts › Collections and streams
Legacy collections: Vector, Hashtable, Enumeration
Vector, Hashtable, Stack, Enumeration and StringTokenizer date from Java 1.0.
Learn these first
At a glance
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.
- 1Vector is a thread-safe ArrayList (each method synchronized); Hashtable is a synchronized HashMap that forbids null keys and valuesStack extends Vector.
- 2Enumeration is the old iterator: while (e.hasMoreElements()) { e.nextElement(); }Convert with Collections.list(enumeration) or enumeration.asIterator().
- 3StringTokenizer splits text by delimiters: while (t.hasMoreTokens()) t.nextToken()It is discouraged: use String.split or Scanner.
- 4Servlets, Properties (a Hashtable), javax.naming and some JDBC and mail APIs still return these types
- Synchronized methods do not make compound operations atomic, and they add lock cost to single-threaded code
- 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
- 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
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<>(); }}- line 3 uses Hashtable, the core of Legacy collections: Vector, Hashtable, Enumeration
- line 4 uses Enumeration, the core of Legacy collections: Vector, Hashtable, Enumeration
- line 5 uses hasMoreElements, the core of Legacy collections: Vector, Hashtable, Enumeration
- line 6 uses nextElement, the core of Legacy collections: Vector, Hashtable, Enumeration
- line 11 uses StringTokenizer, the core of Legacy collections: Vector, Hashtable, Enumeration
- 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
Hashtablefor thread safety and then doing check-then-act on it. - Storing null in a
Hashtable. - Using
StringTokenizerwhere 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:
- Types:
VectorHashtableEnumerationStringTokenizerDictionary - Method calls:
hasMoreElements()nextElement()hasMoreTokens()nextToken()elements() - Created with new:
VectorHashtableStringTokenizer
Part of a learning path
- Collections in depth · previous: The Collections utility class
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.