How to get HashMap into SynchronisedHashMap






Here   is  an   easy example  that  converts  HashMap into SynchronisedHashMap. Its  simple java
 code but  some time useful while handling multi Threads.
     

import java.util.HashMap;
import java.util.Collections;
import java.util.Map;
import  java.util.Set;
import  java.util.Iterator ;


class synchroHashMap
{
  public static void main(String[] args)
{
        HashMap hashMap = new HashMap();

             hashMap.put("1","One");
             hashMap.put("2","Two");
             hashMap.put("3","Three");
Map map = Collections.synchronizedMap(hashMap);

             // map  can be use by mutipul Threads.

                Set  s=hashMap.keySet();

    Iterator  it=s.iterator();

        while(it.hasNext())

System.out.println("Key are " +it.next());
        
           }
          }


   Program  Output . 

 Converted  HashMap into SynchronisedHashMap   and  then  got  the Keyset  of  HashMap. 



Key are 1
Key are 2
Key are 3

How to get HashMap into SynchronisedHashMap    How  to   get  HashMap into SynchronisedHashMap Reviewed by Mukesh Jha on 4:40 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.