Talking about HashMap and Hashtable

             The two words TT made Lao Tzu's eyes dazzled. . . 

Simply put this sentence:

         The 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 solve problems with a variety of ideas . How HashMap works, ArrayList vs Vector, and this question are the most classic questions about the Java Collections framework. Hashtable is an outdated collection class that has existed in the Java API for a long time. It was rewritten in Java 4 to implement the Map interface, so it has since become part of the Java Collections Framework . Hashtable and HashMap are fairly easy to ask in Java interviews and even become the most frequently asked question in Collections framework interview questions, so don't forget to prepare for this question before taking any Java interview.

The difference between HashMap and Hashtable

        Both HashMap and Hashtable implement the Map interface, but before deciding which one to use, you need to figure out the difference between them .

        The main differences are: thread safety, synchronization, and speed.

        1. HashMap is almost equivalent to Hashtable , except that HashMap is asynchronous and can accept null ( HashMap can accept null keys and values, while Hashtable cannot).

        2. HashMap is non-synchronized, and Hashtable is synchronized, which means that Hashtable is thread-safe, and multiple threads can share a Hashtable ; and if there is no correct synchronization, multiple threads cannot share HashMap . Java 5 provides ConcurrentHashMap, which is a replacement for HashTable and is more scalable than HashTable .

        3. Another difference is that the iterator of HashMap is a fail-fast iterator, while the enumerator iterator of Hashtable is not fail-fast. So when other threads change the structure of HashMap (add or remove elements), ConcurrentModificationException will be thrown, but the remove() method of the iterator itself will not throw 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.

        4. 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 is better than Hashtable .

        5. HashMap does not guarantee that the order of elements in the Map will remain unchanged over time.


Points to note:

1, synchronized (synchronized): means that only one thread can change the Hashtable at a time . That is to say, when any thread wants to update the Hashtable , it must first obtain the synchronization lock, and other threads must wait until the synchronization lock is released to obtain the synchronization lock again to update the Hashtable .

2. Fail-safe is related to iterator iterators: 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. But 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, an IllegalArgumentException will be thrown.

3. Structural changes: refers to deleting or inserting an element, which will affect the structure of the map.


HashMap can be synchronized by the following statement:

Map m = Collections.synchronizeMap(hashMap);


Summarize:

The main difference between Hashtable and HashMap is thread safety and speed.

Use Hashtable only if you need full thread safety , and if you use Java 5 or above, ConcurrentHashMap is recommended

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325325499&siteId=291194637