Java Collections .synchronizedSortedMap ( SortedMap <K,V> m)

Syntax

Collections.synchronizedSortedMap(SortedMap <K,V> m) has the following syntax.

public static <K,V> SortedMap <K,V> synchronizedSortedMap(SortedMap <K,V> m)

Example

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


// w w  w  . j a va  2s . c  om
import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

public class Main {
   public static void main(String[] args) {
      // create map
      SortedMap<String,String>  map = new TreeMap<String, String>  ();
      
      // populate the map
      map.put("1","A"); 
      map.put("2","B");
      map.put("3","from java2s.com");
      
      // create a sorted map
      SortedMap<String,String> sortedmap = 
          (SortedMap<String,String>)Collections.synchronizedSortedMap(map);
     
      System.out.println("Synchronized sorted map is :"+sortedmap);
   }
}

The code above generates the following result.