HashTable and HashMap For Programmers(II)

The difference between HashMap and HashTable

HashMap and Hashtable both implement the Map interface, but before deciding which one to use, you need to know the difference between them. The main differences are Thread security, Synchronization, and Speed.tin plate suppliers

HashMap is almost equivalent to Hashtable, except HashMap is non-synchronized, however it can accept null (HashMap can accept null’s key and value).

Hashtable is synchronous, which means that Hashtable is thread-safe, and multiple threads can share a Hashtable; without proper synchronization, multiple threads cannot share a HashMap.

Another difference is that the Iterator of HashMap is a fail-fast iterator, while the enumerator iterator of Hashtable is not a fail-fast iterator. So when other threads change the structure of HashMap (adding or removing elements), a Concurrent ModificationException will be thrown, but the removal () method of the iterator itself will not throw a Concurrent ModificationExceptionException.

Because Hashtable is threaded and synchronized, it is slower than HashMap in a single-threaded environment. If you don’t need synchronization and only need a single thread, using HashMap performs better than Hashtable.

Leave a Reply

Your email address will not be published. Required fields are marked *