Lesson · Strings & Text
Legacy adjacent classes: what to recognise, what to avoid
9 min readLegacy & Adjacent
StringTokenizer, Hashtable, Vector, Stack, Date: the pre-Collections, pre-NIO survivors. Recognise them in old code, choose their modern replacements in new code.
StringTokenizer and the old text tools
StringTokenizer predates regex-based split and the streams era: new StringTokenizer(line, ",") walks delimiter-separated pieces with hasMoreTokens and nextToken. It never returns empty tokens between adjacent delimiters, cannot produce regex matches, and has no way to keep the delimiters. split(",", -1) and Pattern.split are the modern equivalents with explicit, richer semantics.
StringBuffer is the other old text class, covered in the mutable-text topic: recognised by its synchronised methods, replaced everywhere by StringBuilder. The pattern in both cases is the same: the old class works, has subtler behaviours, and new code should prefer the class designed after 2000.
Vector, Stack, Hashtable
Vector (1995) is a synchronised ArrayList; Stack extends Vector with push and pop; Hashtable is a synchronised HashMap that rejects null keys and values. All three synchronise per method for thread safety an application rarely actually needs, at a cost every caller pays. The modern replacements are ArrayList, ArrayDeque (for stacks: push, pop, peek on the front) and HashMap (via Collections.synchronizedMap or ConcurrentHashMap when sharing is real).
Stack has the double dishonour of being a class (extend, inherit the Vector surface, break invariants) where the modern replacement is a Deque interface, and of having its iteration order top-down while the class name suggests otherwise. Old code holding Stack works; new code writes Deque<T> stack = new ArrayDeque<>().
Date, Calendar and the modern time API
java.util.Date is (despite the name) a millisecond instant with a mutable clock reading inside; Calendar is a mutable, month-zero-indexed, not-thread-safe wrapper for doing arithmetic on it. Both were superseded by java.time (Java 8): Instant, LocalDate, LocalDateTime, ZonedDateTime, Duration, all immutable and carefully specified. New code should simply not use Date or Calendar, and old code should translate at the boundary.
Recognising the pair in old signatures still matters: methods returning Date are common in decades-old libraries, and Date.from(instant) and date.toInstant() are the conversion points. The month arithmetic that Calendar gets wrong (JANUARY is 0) is exactly the class of bug java.time was designed to make impossible.
The recognition rule
A glance at the imports tells the decade: java.util.Date, Calendar, Vector, Stack, Hashtable, StringTokenizer, StringBuffer are the survivors of the 1.0 API, kept for binary compatibility, encumbered by the world they shipped into. Knowing each one by sight lets you read old code accurately and, more usefully, avoids importing the pattern into new code out of familiarity.
The same instinct applies going forward: this chapter's own subjects (StringBuffer over StringBuilder, printStackTraces over logging, synchronized everywhere) will age too. Recognition, not nostalgia, is the skill.
Vector is synchronised; ArrayList is not, and that is the point
Per-method synchronisation gives no compound-operation safety anyway (check-then-act races across two calls remain possible), while charging every single-threaded caller. That combination, useless locks plus universal cost, is why the early collections were replaced. Reach for concurrency tools (ConcurrentHashMap, collections wrappers) when sharing actually exists.
Interview angles
Why replace Vector with ArrayList?+
Vector synchronises every method, which is useless compound-operation protection and pure overhead for the common single-threaded case. ArrayList is the same structure without the tax; concurrency, when needed, is handled by explicit wrappers or ConcurrentHashMap.
What replaced Date?+
java.time (Java 8): Immutable, clearly separated types for instant (Instant), date (LocalDate), zoned date-time (ZonedDateTime) and duration. Date and Calendar remain for compatibility with old APIs; conversions happen at the boundary.
StringTokenizer versus split?+
StringTokenizer returns only non-empty tokens in delimiter order and cannot preserve delimiters or return empties. split uses regexes with the limit-controlled trailing-empty behaviour. Modern code uses split or Pattern; StringTokenizer survives in legacy parsers.