Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

A large codebase still has hundreds of uses of Vector, Hashtable and Enumeration, some in public APIs used by other teams, and some required by old APIs such as java.util.Properties and JNDI's InitialContext environment. How do you plan their removal, and where do you stop?

I would classify every use into three groups: private implementation details, which are safe to replace; public API signatures, which need a new method and a deprecation period; and uses required by JDK or framework APIs such as Properties, which extends Hashtable, and the Hashtable environment of JNDI's InitialContext, which we keep but isolate behind Map-based wrappers. Before replacing any Vector or Hashtable, we check whether the code relied on its synchronization, and we automate the mechanical changes with IDE refactorings or OpenRewrite, add a static analysis rule against new uses, and prioritise hot paths and public APIs, stopping where old code is cheaper to leave until it is rewritten.

Why this question is asked

Legacy collections in a large codebase are rarely a single problem. Some are harmless leftovers inside methods, some are part of contracts that other teams compile against, and some are required by APIs we do not control. A plan that treats them all the same either breaks other teams or never finishes. Interviewers ask this to see whether we can scope a clean-up, protect the people who depend on our code, avoid introducing concurrency bugs, and know when to stop. In this answer, we will classify the uses, handle each class of use, look at the hidden thread-safety question, automate the work, and decide where to stop.

1Classify Every Use

The first task is an inventory. A search of the codebase for Vector, Hashtable, Stack and Enumeration, or better a static analysis report, lists every use with its location. Then each use goes into one of three groups, because each group needs a completely different treatment.

Private implementation details are local variables, private fields and private methods. Nothing outside the class sees them, so they can be replaced freely, subject to the thread-safety check below. Public API signatures are public or protected methods, fields and constructor parameters in code other teams depend on. Changing them breaks callers, so they need a migration path. Required uses are places where a JDK or framework API demands the legacy type, and those we cannot remove at all.

Step throughDeciding what to do with each use
  1. Find a legacy collection useVector, Hashtable, Stack or Enumeration
  2. Required by a JDK or framework API?Properties, JNDI, servlet headers, Swing models
    YesKeep it; wrap at the edgeNoContinue
  3. Visible outside the class or module?Public signature used by other teams
    YesAdd new method, deprecate old oneNoReplace directly
  4. Was its synchronization relied on?Shared between threads?
    YesUse ConcurrentHashMap or explicit lockingNoUse ArrayList, HashMap, ArrayDeque

4 steps in order. Play them, or step through them one at a time.

2Keep and Isolate the Required Uses

Some legacy types are baked into APIs that will not change. java.util.Properties extends Hashtable<Object, Object>. JNDI's InitialContext takes its environment as a Hashtable<?, ?>. ClassLoader.getResources returns an Enumeration, and so do older servlet methods such as getHeaderNames. Swing's DefaultTableModel uses Vector. Removing these is impossible, so the goal is to stop them spreading: convert at the edge, so the rest of the code works with Map, List and Iterator.

Java
// JNDI needs a Hashtable; the rest of the code builds a Map
Context lookupContext(Map<String, String> settings) throws NamingException {
Hashtable<String, String> env = new Hashtable<>(settings); // only here
return new InitialContext(env);
}
// Enumeration from an old API, converted once
List<URL> urls = Collections.list(loader.getResources("META-INF/plugin.properties"));
Iterator<String> headers = request.getHeaderNames().asIterator(); // Java 9+
// Properties as a typed Map at the edge
Map<String, String> config = props.stringPropertyNames().stream()
.collect(Collectors.toMap(k -> k, props::getProperty));

Each conversion lives in one small method, so the legacy type appears in exactly one place per API. This is where the plan deliberately stops: these uses are correct, and replacing them would only add complexity.

3Check for Reliance on Synchronization

This step is what makes the clean-up safe. Vector and Hashtable synchronize every method. If a field of that type is shared between threads, replacing it with ArrayList or HashMap removes the locking and can introduce a real concurrency bug: a HashMap updated by two threads can lose entries or corrupt its internal structure. Such bugs are rare under test and show up only under production load.

So for every field we ask whether more than one thread can reach it: a field of a singleton bean, a static field, an object passed to an executor. If it is confined to one thread, such as a local variable or a request-scoped object, plain ArrayList or HashMap is right. If it is shared, the replacement is ConcurrentHashMap, a CopyOnWriteArrayList, or explicit locking that also covers compound operations, which the old class never protected anyway. Remember also that ConcurrentHashMap, like Hashtable, rejects nulls, but HashMap does not, so the null behaviour can change too.

Java
// Before: a shared cache in a singleton
private final Hashtable<String, Rate> rates = new Hashtable<>();
// Wrong replacement: unsafe when several request threads write
private final Map<String, Rate> rates = new HashMap<>();
// Right replacement, and the compound operation becomes atomic too
private final Map<String, Rate> rates = new ConcurrentHashMap<>();
Rate rate = rates.computeIfAbsent(currency, this::loadRate);

4Migrate Public APIs Through Deprecation

For public signatures, we add a new method that uses the modern type, make the old method delegate to it, and deprecate the old one with a clear message and a removal timeline. Callers migrate at their own pace, and we can track who still calls the old method.

Java
/** @deprecated use {@link #items()}, which returns an unmodifiable List; removal in 5.0 */
@Deprecated(since = "4.2", forRemoval = true)
public Vector<Item> getItems() {
return new Vector<>(items()); // old contract: a fresh, modifiable Vector
}
public List<Item> items() {
return List.copyOf(items);
}

Notice that the old method keeps its old contract, returning a fresh Vector that callers may have been modifying. The new method returns an unmodifiable list, so we document that difference. With forRemoval = true, the compiler warns every caller, and in the next major version we remove the method.

5Automate, Prevent New Uses and Prioritise

The private replacements are mechanical, so we should not do them by hand across hundreds of files. IDE refactorings handle single classes, and OpenRewrite can apply a recipe across many repositories; check its catalogue for existing recipes, or write a small custom one, and review the changes that touch shared fields carefully. To stop new uses from appearing, we add a static analysis rule: PMD ships rules that flag Vector, Hashtable and Enumeration, and an ArchUnit test or Error Prone check can do the same, with an allow list for the required API edges.

Finally, we prioritise by value. Public APIs come first, because every month they stay, more callers depend on them. Hot paths come next, where removing unnecessary locking or replacing a contended Hashtable with ConcurrentHashMap can improve throughput. Old code that works, rarely changes and is scheduled for a rewrite can be left alone: the rule against new uses keeps the problem from growing, and the remaining uses disappear when that code is replaced. Stopping there is a deliberate choice, not an unfinished job.

6Follow-up questions

How would you deprecate a public method that returns Vector?

Add a new method returning List, ideally an unmodifiable one, and make the old method delegate to it, wrapping the result in a new Vector so its contract stays the same. Mark the old method @Deprecated(since = ..., forRemoval = true) with Javadoc pointing to the replacement and the planned removal version, announce the timeline to the consuming teams, track remaining callers, and remove it in the next major release.

Can we replace Stack with ArrayDeque automatically?

Mostly, because push, pop and peek have the same names. But Stack allows null elements and ArrayDeque does not, and code that used Stack's list methods, such as get(index) or iteration order, behaves differently: Stack iterates from bottom to top, while ArrayDeque used as a stack iterates from top to bottom. Those uses need a manual check.

How do you know when the clean-up is finished?

When the static analysis rule reports only allowed uses at API edges, public APIs no longer expose legacy types, and the remaining internal uses are in code with no planned changes. The metric is the trend, not zero.

7Conclusion

In this answer, we planned the removal of legacy collections by classifying each use as a private detail, a public API or a required JDK or framework use. Required uses such as Properties and the JNDI environment stay, wrapped so the rest of the code uses Map. Private uses are replaced after checking whether their synchronization was relied on, public methods are migrated through deprecation, the mechanical work is automated with IDE refactorings or OpenRewrite, new uses are blocked by static analysis, and we stop where old code is cheaper to leave until it is rewritten.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start with an inventory and sort every use into three groups. First, private implementation details, like local variables and private fields; those we can replace. Second, public API signatures that other teams use; changing those breaks callers, so they need a new method and a deprecation period.

Then, if they want more

Third, uses required by JDK or framework APIs, like Properties, which extends Hashtable, JNDI's InitialContext, which takes a Hashtable environment, or old APIs that return Enumeration; those we keep, but wrap at the edge so the rest of the code sees Map or Iterator.

Before replacing a Vector or Hashtable, the key question is whether the code relied on its synchronization. If the field is shared between threads, swapping it for HashMap introduces a race, so the replacement there is ConcurrentHashMap or explicit locking, and I would watch the null rules too.

For public methods, I add a new method returning List, make the old one delegate and wrap in a new Vector, mark it deprecated for removal, and remove it in the next major version. The mechanical part I would automate with IDE refactorings or OpenRewrite, and I would add a PMD or ArchUnit rule so no new uses appear.

I would prioritise public APIs and hot paths. And I would stop where old, stable code is cheaper to leave until it is rewritten, because the rule keeps the problem from growing.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A large codebase still has hundreds of uses of Vector, Hashtable and Enumeration, some...": https://scrivio-interview-library.netlify.app/java/collections-overview/37539f3762/