ReviseAlgo Logo

Collections Framework

Hashtable

Analyze Hashtable legacy synchronized map design, method structures, and null exclusions.

Interview: Focuses on method-level synchronization overhead, null key/value rejection, and replacement with ConcurrentHashMap.

Last Updated: June 13, 2026 10 min read

A Hashtable is a legacy hash table implementation that maps keys to values. All methods are synchronized at the method level, which introduces lock acquisition overhead. Unlike HashMap, Hashtable does not permit null keys or values.

Synchronized

Every data operation uses the object monitor lock, blocking other threads from accessing the Hashtable.

Null Rejection

Throws a NullPointerException if a null key or value is passed, as the hashing function requires non-null objects.

Legacy API

Exposes older Enumeration iterators alongside standard Map iterators.

Hashtable vs HashMap vs ConcurrentHashMap

Hashtable has been superseded by more modern map implementations:

  • Single Threaded: Use HashMap instead of Hashtable to avoid synchronization overhead.
  • Concurrent access: Use ConcurrentHashMap instead of Hashtable. ConcurrentHashMap uses lock striping and CAS operations to allow multiple threads to access the map concurrently without blocking.

Common Pitfalls

  • Inserting nulls: Passing null keys or values to put, which raises a NullPointerException.
  • Unnecessary lock contention: Sharing a Hashtable among multiple threads for read-only access, which blocks threads needlessly due to synchronized read methods.

Best Practices

  • Avoid Hashtable: Treat Hashtable as a legacy class. Use ConcurrentHashMap for concurrent applications and HashMap for single-threaded code.

Interview-Relevant Information

Q1: What are the differences between Hashtable and HashMap?
Answer: 1) Hashtable is synchronized, whereas HashMap is not. 2) Hashtable does not permit null keys or values, while HashMap allows one null key and multiple null values.

Q2: Why is ConcurrentHashMap preferred over Hashtable?
Answer: Hashtable locks the entire map during access, which blocks other threads. ConcurrentHashMap uses lock striping or CAS operations to lock only individual map segments or nodes, allowing concurrent access and improving throughput.

Quick Checklist

Can you explain why Hashtable is synchronized, state its null handling rules, and identify its modern concurrent alternative? If yes, you understand Hashtable.

Use Cases

Integrating with older legacy enterprise systems written prior to the introduction of JCF.

Quick single-object synchronization prototypes where thread contention is absent.

Common Mistakes

Attempting to store null keys or values in a Hashtable.

Using Hashtable in modern, high-throughput concurrent systems.