Java Collection Tutorial - Java Collections .synchronizedMap ( Map <K,V> m)








Syntax

Collections.synchronizedMap(Map <K,V> m) has the following syntax.

public static <K,V> Map <K,V> synchronizedMap(Map <K,V> m)

Example

In the following code shows how to use Collections.synchronizedMap(Map <K,V> m) method.

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/* w w w.j  a va 2 s. c om*/
public class Main {
   public static void main(String[] args) {
      // create map
      Map<String,String>  map = new HashMap<String,String> ();
      
      // populate the map
      map.put("1","A"); 
      map.put("2","B");
      map.put("3","java2s.com");
      
      // create a synchronized map
      Map<String,String>  synmap = Collections.synchronizedMap(map);
     
      System.out.println("Synchronized map is :"+synmap);
   }
}

The code above generates the following result.