java.util.Collections.synchronizedSortedMap() Method


Description

The synchronizedSortedMap() method is used to returns a synchronized (thread-safe) sorted map backed by the specified sorted map.

Declaration

Following is the declaration for java.util.Collections.synchronizedSortedMap() method.

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

Parameters

m − This is the sorted map to be "wrapped" in a synchronized sorted map.

Return Value

  • The method call returns a synchronized view of the specified sorted map.

Exception

NA

Example

The following example shows the usage of java.util.Collections.synchronizedSortedMap()

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String[] args) {
   
      // create map
      SortedMap<String,String> map = new TreeMap<String, String> ();

      // populate the map
      map.put("1","TP"); 
      map.put("2","IS");
      map.put("3","BEST");

      // create a sorted map
      SortedMap sortedmap = (SortedMap)Collections.synchronizedSortedMap(map);

      System.out.println("Synchronized sorted map is :"+sortedmap);
   }
}    

Let us compile and run the above program, this will produce the following result.

Synchronized sorted map is :{1=TP, 2=IS, 3=BEST}
java_util_collections.htm
Advertisements