Tips and Tricks SQL PL/SQL HTML CSS Web services(SOAP/REST) ANT MAVEN XML JSON Ajax Materials Eclipse Tricks Server Confiqurations Bootstrap

Sunday, 13 April 2014

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();
Iterator itr=list1.iterator();
list1.add("asif");   //here you will get ConcurrentModificationException
System.out.println(; //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();
System.out.println("Vector Object is");
Enumeration e=v.elements();
v.add("rajesh");     //here you won't get any exception.

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..."); 

map.put(4, "Rakesh"); //concurrent modificationexception... 

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);
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...");
concurrentMap.put(4, "Rock");  //here one element is added at the time of retrieval,but no exception
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 :-)


Powered by Jasper Roberts - Blog