Home >Java >javaTutorial >The difference between HashMap and Hashtable in JAVA
Comparison of HashMap and Hashtable is a common question in Java interviews. It is used to test whether programmers can use collection classes correctly and whether they can adapt to different situations and use a variety of ideas to solve problems. The working principle of HashMap, the comparison between ArrayList and Vector, and this question are the most classic questions about the Java collection framework. Hashtable is an obsolete collection class that has existed in the Java API for a long time. It was rewritten in Java 4 and implemented the Map interface, so it has become part of the Java collection framework since then. Hashtable and HashMap are quite easy to be asked in Java interviews, and have even become the most commonly asked questions in collection framework interview questions, so don't forget to prepare for this question before participating in any Java interview.
In this article, we will not only see the difference between HashMap and Hashtable, but also the similarities between them.
The difference between HashMap and Hashtable
HashMap and Hashtable both implement the Map interface, but before deciding which one to use, you must first understand the difference between them. The main differences are: thread safety, synchronization, and speed.
HashMap is almost equivalent to Hashtable, except that HashMap is non-synchronized and can accept null (HashMap can accept null key and value, but Hashtable cannot).
HashMap is non-synchronized, while Hashtable is synchronized, which means that Hashtable is thread-safe and multiple threads can share a Hashtable; without correct synchronization, multiple threads cannot share HashMap. Java 5 provides ConcurrentHashMap, which is a replacement for HashTable and has better scalability than HashTable.
Another difference is that HashMap's iterator (Iterator) is a fail-fast iterator, while Hashtable's enumerator iterator is not fail-fast. So when other threads change the structure of the HashMap (add or remove elements), a ConcurrentModificationException will be thrown, but the remove() method of the iterator itself will not throw a ConcurrentModificationException when removing elements. But this is not a guaranteed behavior, it depends on the JVM. This is also the difference between Enumeration and Iterator.
Because Hashtable is thread-safe and synchronized, it is slower than HashMap in a single-threaded environment. If you don't need synchronization and only need a single thread, then using HashMap will perform better than Hashtable.
HashMap cannot guarantee that the order of elements in the Map will remain unchanged over time.
Some important terms to note:
1) sychronized means that only one thread can change the Hashtable at a time. That is to say, any thread that wants to update the Hashtable must first obtain the synchronization lock, and other threads must wait until the synchronization lock is released before they can obtain the synchronization lock again and update the Hashtable.
2) Fail-safe is related to iterator. If a collection object creates an Iterator or ListIterator, and then other threads try to "structurally" change the collection object, a ConcurrentModificationException will be thrown. However, it is allowed that other threads can change the collection object through the set() method, because this does not "structurally" change the collection. But if the structure has been changed and the set() method is called again, an IllegalArgumentException will be thrown.
3) Structural changes refer to deleting or inserting an element, which will affect the structure of the map.
Can we synchronize HashMap?
HashMap can be synchronized through the following statement:
Map m = Collections.synchronizeMap(hashMap);
Conclusion
There are several major differences between Hashtable and HashMap : Thread safety and speed. Only use Hashtable if you need complete thread safety, and if you are using Java 5 or above, use ConcurrentHashMap.
The above is the detailed content of The difference between HashMap and Hashtable in JAVA. For more information, please follow other related articles on the PHP Chinese website!