Home  >  Article  >  Java  >  The difference between HashMap VS. HashTable in Java

The difference between HashMap VS. HashTable in Java

巴扎黑
巴扎黑Original
2016-12-05 10:30:461136browse

The comparison between 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 use a variety of ideas to solve problems according to circumstances. 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 frequently 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

Both HashMap and Hashtable 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.

1. HashMap is almost equivalent to Hashtable, except that HashMap is non-synchronized.

2. HashMap can accept null keys and values, but Hashtable cannot.

3. HashMap is non-synchronized, while Hashtable is synchronized.
This 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.

4. 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 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.

5. Since 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.

6. HashMap cannot guarantee that the order of elements in the Map will remain unchanged over time.
So during iteration, if the HashMap is modified, an exception will be thrown. Because some elements may not be iterated.



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:

Java code

Map m = Collections.synchronizeMap(hashMap);





Conclusion

There are several main ones between Hashtable and HashMap Difference: thread safety and speed. Only use Hashtable if you need complete thread safety, and if you are using Java 5 or above, use ConcurrentHashMap.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:java syntax basicsNext article:java syntax basics