Course contentsShow
← Java
Lesson 977 of 2,87023. Collections Framework OverviewFree lesson

Legacy Collection Classes: Vector and Hashtable

Recognize older synchronized collections and why modern alternatives like ArrayList and HashMap are preferred.

Legacy Collection Classes: Vector and Hashtable

What you'll learn: You'll understand why Vector and Hashtable exist in Java but are rarely the right choice for modern code.

The Old Guard of Collections

Before Java introduced the modern Collections Framework (in Java 1.2), programmers used Vector and Hashtable for dynamic arrays and key-value storage. These classes are synchronized, meaning every method automatically locks the collection to ensure thread safety when multiple threads access it.

Think of it like a bathroom with an auto-locking door: only one person can use it at a time, even if everyone waiting is from the same family and could coordinate themselves.

Why They're Called "Legacy"

While synchronization sounds good, it comes with a performance cost. Every single operation pays this penalty, even when your program only uses one thread (which is extremely common).

Modern alternatives:

  • Use ArrayList instead of Vector
  • Use HashMap instead of Hashtable

These modern classes are faster because they're not synchronized by default. If you need thread safety, you can add it explicitly using wrapper methods or concurrent collections—giving you control over when to pay the synchronization cost.

// Legacy (avoid in new code)
Vector<String> oldWay = new Vector<>();
Hashtable<String, Integer> oldMap = new Hashtable<>();

// Modern approach
ArrayList<String> newWay = new ArrayList<>();
HashMap<String, Integer> newMap = new HashMap<>();

When You'll See Them

You might encounter Vector and Hashtable in older codebases or legacy APIs, so it's important to recognize them. They're not "broken"—they work correctly—but they represent outdated design patterns.

Key Takeaway: Vector and Hashtable are legacy synchronized collections that have been superseded by ArrayList and HashMap, which offer better performance and more flexible thread-safety options.