1. Introduction
HashMap and Hashtable in Java are pretty similar fundamentally. While both use the same hashing technique and implement the Map interface for unique key storage using bucketing segregation according to a hash value, this article dives into when it’s best suited to choose one over another.
2. HashMap vs Hashtable in Java
Due to the hashing technique, both data structures achieve constant-time performance when inserting, removing and retrieving elements. It’s possible to use the HashMap implementation of TreeMap to sort the records according to ascending order of keys. LinkedHashMap is another implementation that will maintain the insertion order. Hashtable doesn’t provide any of those functions. More about different map implementations here.
2.1 Multithreaded Environment
The most significant difference between HashMap and Hashtable is that Hashtable is synchronised, and HashMap is not.
Hashtable thread safety allows concurrent modifications by multiple threads. HashMap is unsuitable in a multithreaded environment without additional synchronisation code, and it is significantly faster while consuming less memory.
Hashtable is a legacy class, so it has limited synchronisation capabilities; quite often, you will need to implement some extra logic to achieve it fully.
2.2 Iteration Over Elements
Hashtable can use an iterator and enumerator to iterate over elements. Iterator has the advantage due to being fail-fast, whereas enumerator is not fail-fast.
Hashtable<String, String> hashtableElements = new Hashtable<>();
hashtableElements.put("ElementOne", "One");
hashtableElements.put("ElementTwo", "Two");
hashtableElements.put("ElementThree", "Three");
Enumeration<String> hashtableEnumerator = hashtableElements.elements();
while (hashtableEnumerator.hasMoreElements()) {
hashtableEnumerator.nextElement();
hashtableElements.put("ElementFour", "Four");
}
When using an enumerator is possible to add elements while iterating.
Map<String, String> hashMapElements = new HashMap<>();
hashMapElements.put("ElementOne", "One");
hashMapElements.put("ElementTwo", "Two");
hashMapElements.put("ElementThree", "Three");
Iterator<String> hashMapIterator = hashMapElements.keySet().iterator();
while (hashMapIterator.hasNext()) {
hashMapIterator.next();
hashMapElements.put("ElementFour", "Four");
}
HashMap can only use an iterator that is fail-fast, so adding any elements while iterating over it will throw ConcurrentModificationException.
2.3 Insertion of Null Elements
hashMapElements.put(null, "null");
System.out.println(hashMapElements.size()); // 4
hashtableElements.put(null, "null"); // java.lang.NullPointerException
Null element handling is another difference between the two data structures. Hashtable doesn’t allow null elements in key or value and will throw NullPointerException if you try to add one. HashMap will allow a single null key element and multiple null values.
3. When to use HashMap over Hashtable?
Hashtable is a legacy class with limited synchronisation that only prevents multiple threads from adding and removing from the map concurrently. That may not be enough in the real world, and a more modern approach of putIfNotPresent may be required. This is already accomplished in ConcurrentHashMap, so consider using that instead in a concurrent environment.
You should favour HashMap in single-threaded applications due to performance and memory improvements.
4. Summary
What’s the difference between HashMap and hashtable? Age matters here – with a whopping 23 years age gap, it can be challenging to keep them straight. The key takeaway is to use HashMap in single-threaded apps but switch to thread-safe alternatives, e.q. ConcurrentHashMap for anything multi-threaded!
Daniel Barczak
Daniel Barczak is a software developer with a solid 9-year track record in the industry. Outside the office, Daniel is passionate about home automation. He dedicates his free time to tinkering with the latest smart home technologies and engaging in DIY projects that enhance and automate the functionality of living spaces, reflecting his enthusiasm and passion for smart home solutions.
Leave a Reply