What is ConcurrentHashMap concept in java? Difference B/W HashMap and ConcurrentHashMap ?

   Hi friends Today i came up with very good concept ConcurrentHashMap in java and also very important for  interviews. They may ask like what is the difference between HashMap and ConcurrentHashMap ? for this question we will see the explanation  below . For clear understanding purpose i will explain with programmatically. So, let's go.

  Whenever you iterate over the collection object That is retrieving the elements from the object at the same time you cannot perform another operations like removing elements from the object, adding elements to the object which does not have the thread safe capability. That is in collection implemented classes except vector like ArrayList,LinkedList e.t.c we cannot modify the object at the time of retrieving the elements from object. But we can perform in Vector class why because vector is Thread safe.For clear explanation we will see with programatically .

import java.util.*;
public class Test1{
public static void main(String args[]){
List list1=new ArrayList();
list1.add("Raju");
list1.add("Rani");
list1.add("Ramesh");
Iterator itr=list1.iterator();
while(itr.hasNext()){
list1.add("asif");   //here you will get ConcurrentModificationException
System.out.println(itr.next()); //retrieving the elements from the object.
}
}
}

But, where as at the time of retrieving elements from vector you won't get such kind of exception because we already know vector is thread safe.

Vector v=new Vector();
v.add("Raju");
v.add("Rani");
v.add("Ramesh");
System.out.println("Vector Object is");
Enumeration e=v.elements();
while(e.hasMoreElements()){
v.add("rajesh");     //here you won't get any exception.
System.out.println(e.nextElement());
}

Similarly with Map also will get ConcurrentModificationException.Map is a key and value pairs of object,there is no method directly in the Map to retrieve the elements from the Map object.We need to separately retrieve keys and values from the Map object.
import java.util.*
public class Test2{
public static void main(String args[]){
Map map=new HashMap(); 
map.put(1, "Raju");
map.put(2, "Rani"); 
map.put(3, "Ramesh"); 
System.out.println("Map Values==> "+map); 
Set keySet=map.keySet(); //It will return only keys of Map object as Set. 
Collection values= map.values(); //It will return only values of Map object as Collection. 
System.out.println("Key Set is==> "+keySet);
System.out.println("Values==> "+values);
Iterator itr=keySet.iterator(); 
System.out.println("Map values through iterator..."); 
while(itr.hasNext())

map.put(4, "Rakesh"); //concurrent modificationexception... 
System.out.println(itr.next());  
}
}
}

You may get one doubt here Why keys of Map as Set and values of Map as Collection, because keys can't duplicated in Map and values can be duplicated.Set doesn't allow duplicate values where as Collection allows duplicate values.

So as we saw above With the map object we cannot perform adding and deleting the elements from the map object  so in order to achieve this problem ConcurrentHashMap is introduced in 1.5 version of java in java.util.concurrent package.

import java.util.*;
import java.util.concurrent.*;
public class Test3{
public static void main(String args[]){
ConcurrentHashMap concurrentMap=new ConcurrentHashMap();
concurrentMap.put(1, "Ramu");
concurrentMap.put(2, "James");
concurrentMap.put(3, "Jhony");
Set set2=concurrentMap.keySet();
System.out.println("ConcurrentMap key values==> "+set2);
values=concurrentMap.values();
System.out.println("concurrentMap Values==> "+values);
Iterator itr=values.iterator(); //you can iterate over either keys or values..
System.out.println("concurrentMap values through iterator...");
while(itr.hasNext()){
concurrentMap.put(4, "Rock");  //here one element is added at the time of retrieval,but no exception
System.out.println(itr.next());
concurrentMap.remove(2);        // here one element is removed at the time of retrieval,but not exception.
}
System.out.println("All Values==> "+concurrentMap); //here you can see the modified object.
}
}

So, from above two programs you can understand the difference b/w HashMap and ConcurrentHashMap.

The class which can throw ConcurrentModificationException at the time of retrieving the elements from the object,it has failfast property and the class which never throws ConcurrentModificationException at the time of retrieving the elements from the object,then we can say that it has failsafe property.
That's all about it... if you have any doubts regarding this concepts please leave comment below or else contact us. Thank you :-)

Related posts

0 comments: