Dung (Donny) Nguyen

Senior Software Engineer

HashMap and Hashtable

The key differences between HashMap and Hashtable in Java are mainly in terms of synchronization, null handling, and performance. This is the brief comparison table:

Feature HashMap Hashtable
Synchronization Not synchronized (thread-unsafe) Synchronized (thread-safe)
Null keys/values Allows one null key, multiple null values Does not allow null keys/values
Performance Faster (no overhead of synchronization) Slower (due to synchronization)
Introduced Java 1.2 (Collections Framework) Java 1.0 (Legacy class)
Iterator behavior Fail-fast Fail-safe
Preferred for Single-threaded or custom-synchronized Thread-safe (use ConcurrentHashMap instead in most cases)

Here’s a detailed comparison:

1. Thread Safety

2. Null Keys and Values

3. Legacy vs Modern

4. Performance

5. Iterators

6. Substitute for Synchronization

7. Default Capacity and Load Factor

8. Inheritance

Which One to Use?