Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praveenkishor123/f82af0ca96062a754fb7134223e03825 to your computer and use it in GitHub Desktop.
Save praveenkishor123/f82af0ca96062a754fb7134223e03825 to your computer and use it in GitHub Desktop.
Differences between HashMap and HashTable in Java
=====================================================
1. Hashtable is synchronized, whereas HashMap is not. This makes HashMap better for
non-threaded applications, as unsynchronized Objects typically perform better than
synchronized ones.
2. Hashtable does not allow null keys or values. HashMap allows one null key and any
number of null values.
3. One of the HashMap's subclasses is LinkedHashMap, so in the event if we want to
predict the iteration order(which is insertion order by default), we can easily change
our HashMap to LinkedHashMap. This is very difficult to achieve with Hashtable.
If synchronization is not an issue, then use HashMap.
If synchronization is an issue then use ConcurrentHashMap.
How to make a HashMap thread-safe ?
==========================================
use Collections.synchronizedMap()
NOTE
----------
Even iterating over a Hashtable's entries or HashMap obtained by
Collections.synchronizedMap is not thread safe unless we also guard the Map from being
modified through additional synchronization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment