HashMap and Hashtable are two important Map implementations in Java. Both store data in key-value pairs and use hashing internally, but they differ significantly in terms of synchronization, performance, null handling, and modern usage.
Understanding the differences between HashMap and Hashtable is a common interview topic and can help developers select the appropriate collection for their applications.
What is HashMap?
HashMap is a part of the Java Collections Framework introduced in Java 1.2. It provides high-performance data storage using key-value pairs and is not synchronized.
Map<Integer, String> map = new HashMap<>();
map.put(1, "Java");
map.put(2, "Spring");
System.out.println(map);
What is Hashtable?
Hashtable is a legacy collection class introduced before the Collections Framework. It stores data using key-value pairs and provides thread safety through synchronization.
Hashtable<Integer, String> table =
new Hashtable<>();
table.put(1, "Java");
table.put(2, "Spring");
System.out.println(table);
Major Differences Between HashMap and Hashtable
| Feature | HashMap | Hashtable |
|---|---|---|
| Introduced In | Java 1.2 | Java 1.0 |
| Thread Safe | No | Yes |
| Synchronized | No | Yes |
| Performance | Faster | Slower |
| Null Keys | One Allowed | Not Allowed |
| Null Values | Multiple Allowed | Not Allowed |
| Legacy Collection | No | Yes |
Null Key Example in HashMap
HashMap<Integer,String> map =
new HashMap<>();
map.put(null, "Java");
System.out.println(map);
Output
{null=Java}
HashMap allows one null key.
Null Key Example in Hashtable
Hashtable<Integer,String> table =
new Hashtable<>();
table.put(null, "Java");
Output
Exception in thread "main"
java.lang.NullPointerException
Hashtable does not allow null keys.
Why Hashtable Is Slower
Hashtable synchronizes every method. Only one thread can access the object at a time.
public synchronized V put(K key, V value)
Because of synchronization overhead, Hashtable is slower than HashMap in most applications.
Thread Safety Example
Hashtable provides built-in synchronization.
Hashtable<Integer,String> table =
new Hashtable<>();
table.put(1, "Java");
table.put(2, "Spring");
Multiple threads can work safely with Hashtable, although performance may suffer.
HashMap in Multi-Threaded Applications
HashMap is not thread-safe. Concurrent modifications may lead to inconsistent results.
Map<Integer,String> map =
new HashMap<>();
For modern multi-threaded applications, ConcurrentHashMap is generally preferred.
Internal Working
Both HashMap and Hashtable use hashing techniques internally.
The process:
- Calculate hashCode().
- Find bucket location.
- Store the entry in the bucket.
- Use equals() when required.
HashMap received significant performance improvements in Java 8 through treeification. Hashtable did not receive equivalent modern enhancements.
Performance Comparison
| Operation | HashMap | Hashtable |
|---|---|---|
| put() | Fast | Slower |
| get() | Fast | Slower |
| remove() | Fast | Slower |
HashMap is normally preferred when synchronization is not required.
Modern Alternative to Hashtable
Instead of Hashtable, modern Java applications usually use:
ConcurrentHashMap
ConcurrentHashMap provides thread safety with significantly better performance.
ConcurrentHashMap<Integer,String> map =
new ConcurrentHashMap<>();
map.put(1, "Java");
map.put(2, "Spring");
Real Interview Questions
Which is faster: HashMap or Hashtable?
HashMap is generally faster because it does not synchronize every operation.
Is Hashtable thread-safe?
Yes. Hashtable synchronizes its methods and is thread-safe.
Can HashMap store null keys?
Yes. One null key and multiple null values are allowed.
Can Hashtable store null keys?
No. Hashtable throws NullPointerException.
Real World Recommendation
- Use HashMap for single-threaded applications.
- Use ConcurrentHashMap for modern multi-threaded applications.
- Avoid Hashtable in new projects.
- Understand Hashtable because it is still asked in interviews.
Common Interview Questions
Why is Hashtable considered legacy?
Hashtable was introduced before the Collections Framework and has largely been replaced by newer implementations.
Why is HashMap preferred today?
Better performance, support for nulls, and integration with modern collection APIs.
Which collection should replace Hashtable?
ConcurrentHashMap is typically recommended for thread-safe requirements.
Do HashMap and Hashtable use hashing internally?
Yes. Both rely on hashing for data storage and retrieval.
Frequently Asked Questions (FAQ)
Can HashMap be synchronized?
Yes. It can be wrapped using Collections.synchronizedMap().
Map<Integer,String> map =
Collections.synchronizedMap(
new HashMap<>());
Which one should be used in modern applications?
HashMap for non-concurrent applications and ConcurrentHashMap for concurrent applications.
Does Hashtable maintain insertion order?
No. Hashtable does not guarantee insertion order.
Can Hashtable contain duplicate keys?
No. Like all Map implementations, keys must be unique.
Conclusion
HashMap and Hashtable may appear similar, but they serve different purposes. HashMap provides better performance and flexibility, while Hashtable offers built-in synchronization at the cost of speed. In modern Java development, HashMap and ConcurrentHashMap are almost always preferred, but understanding Hashtable remains important for interviews and legacy systems.
Keywords
HashMap vs Hashtable, Difference Between HashMap and Hashtable, Hashtable Interview Questions, HashMap Interview Questions, Thread Safe Collections Java, HashMap vs ConcurrentHashMap, Java Collections Framework, Hashtable Internal Working, HashMap Internal Working, Java Collection Interview Questions, Hashtable Example, HashMap Null Key, Legacy Collections Java, Core Java Interview Questions, Java Map Implementations
Reviewed by 13thGhost
on
3:26 PM
Rating:

No comments: