Created
November 27, 2016 20:35
-
-
Save praveenkishor123/f82af0ca96062a754fb7134223e03825 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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