HashMap :
- HashMap is non-Synchronized.
- HashMap is not Thread-safe.
- HashMap performance is almost high because it is non-synchronized, and threads can perform simultaneously.
- One Thread is Iterating the HashMap Object, if the other Thread tries to add/modify the contents of the Object then we will get a Run-time exception telling ConcurrentModificationException.
- HashMap can allow only one null key.
ConcurrentHashMap :
- ConcurrentHashMap is Synchronized.
- ConcurrentHashMap is Thread-safe.
- ConcurrentHashMap performance is low sometimes because sometimes Threads are required to wait on ConcurrentHashMap.
- ConcurrentHashMap won’t get any exception while doing any modification at the time of Iteration.
- If the specified key or value is NULL, it will throw NullPointerException.
API Reference:
final V putVal(K key, V value, boolean onlyIfAbsent) {
if (key == null || value == null) throw new NullPointerException();
- Please write your valuable comments and feedback below.
Good Post! Keep continuing.
Thanks for your valuable comments.