HashMap vs ConcurrentHashMap
HashMap iterators are fail fast
HashMap vs ConcurrentHashMap

Map Hierarchy
HashMap iterators are fail fast
In the code below we modify the HashMap while iterating it which results in a ConcurrentModificationException.
Map<Integer,String> hm = new HashMap<>();
hm.put(1, "Mangoes");
hm.put(2, "Oranges");
hm.put(3, "Guava");
try {
for(Map.Entry<Integer, String> entry:hm.entrySet())
{
System.out.println(entry.getKey()+" "+entry.getValue());
hm.put(4, "Strawberry");
}
}
catch(Exception e)
{
System.out.println("Exception e "+e);
}
ConcurrentHashMap iterators are fail safe.
The code below does not throw any Exception.
Map<Integer,String> chm = new ConcurrentHashMap<>();
chm.put(1, "Mangoes");
chm.put(2, "Oranges");
chm.put(3, "Guava");
for(Map.Entry<Integer, String> entry:chm.entrySet())
{
System.out.println(entry.getKey()+" "+entry.getValue());
chm.put(4, "Strawberry");
}
2. HashMap allows a null key and multiple null values Vs ConcurrentHashMap doesn’t allow null keys or null values.
Map<Integer,String> hm = new HashMap<>();
hm.put(1, "Mangoes");
hm.put(2, "Oranges");
hm.put(3, "Guava");
hm.put(null, "Apple");
hm.put(null, "Strawberry");
hm.put(4, null);
hm.forEach((k,v)->{System.out.println(k+" - "+v);});
Map<Integer,String> chm = new ConcurrentHashMap<>();
chm.put(1, "Mangoes");
chm.put(2, "Oranges");
chm.put(3, "Guava");
try {
chm.put(null, "Apple");
}catch(Exception e)
{
System.out.println("Exception e "+e);
}
try {
chm.put(null, "Strawberry");
}catch(Exception e)
{
System.out.println("Exception e "+e);
}
try {
chm.put(4, null);
}catch(Exception e)
{System.out.println("Exception e "+e);
}
chm.forEach((k,v)->{System.out.println(k+" - "+v);});
3. HashMap is not thread safe Vs ConcurrentHashMap is thread safe.
HashMap does not implement any concurrency control mechanism. The HashMap size after running the code below might not be 1000 as expected.
public class HMvsCHM2 {
private static final List<String> FRUITSLIST = Arrays.asList("Mango", "Banana", "Orange", "Grapes", "Strawberry",
"Apple", "Pineapple");
public static void main(String[] args) throws InterruptedException {
Map<Integer, String> map = new HashMap<>();
Runnable fruitTask = () -> {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
map.put(i, FRUITSLIST.get(random.nextInt(FRUITSLIST.size())));
}
};
Thread t1 = new Thread(fruitTask,"T1");
Thread t2 = new Thread(fruitTask,"T2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("HashMap size (expected 1000)"+map.size());
}
}

Concurrent HashMap uses CAS and synchronized as concurrency control mechanisms. The ConcurrentHashMap size after running the code below is 1000 as expected.
public class HMvsCHM2 {
private static final List<String> FRUITSLIST = Arrays.asList("Mango", "Banana", "Orange", "Grapes", "Strawberry",
"Apple", "Pineapple");
public static void main(String[] args) throws InterruptedException {
Map<Integer, String> map = new ConcurrentHashMap<>();
Runnable fruitTask = () -> {
Random random = new Random();
for (int i = 0; i < 1000; i++) {
map.put(i, FRUITSLIST.get(random.nextInt(FRUITSLIST.size())));
}
};
Thread t1 = new Thread(fruitTask,"T1");
Thread t2 = new Thread(fruitTask,"T2");
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("ConcurrentHashMap size (expected 1000)"+map.size());
}
}
Concurrent HashMap vs HashMap

Link to Youtube Video :
[embed]
https://gitlab.com/fullstackdevai-group/javabasics
References :
메타데이터
- post_id
- db45be40bccd
- slug
- hashmap-vs-concurrenthashmap-db45be40bccd
- url
- https://medium.com/@fullstackdevai/hashmap-vs-concurrenthashmap-db45be40bccd
- canonical_url
- https://medium.com/@fullstackdevai/hashmap-vs-concurrenthashmap-db45be40bccd
- author_url
- https://medium.com/@fullstackdevai
- status
- ok
- fetched_at
- 2026-08-10 06:06:41