java.util.Collections.checkedSortedMap() Method



The checkedSortedMap(SortedMap<, V>, Class<K>, Class<V>) method is used to get a dynamically typesafe view of the specified sorted map.


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

public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m,Class<K> keyType,Class<V> valueType)


  • m--This is the map for which a dynamically typesafe view is to be returned.

  • keyType--This is the type of key that m is permitted to hold.

  • valueType --This is the type of value that m is permitted to hold.

Return Value

The method call returns a dynamically typesafe view of the specified map.


  • NA


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

package com.tutorialspoint;

import java.util.*;

public class CollectionsDemo {
   public static void main(String args[]) {
      // create sorted map    
      SortedMap smap = new TreeMap();
      // populate the map
      smap.put("1", "Only");
      smap.put("2", "tutorials");
      smap.put("3", "point");
      // get typesafe view of the sorted map
      SortedMap tsmap;
      tsmap = Collections.checkedSortedMap(smap,String.class,String.class);
      System.out.println("Typesafe view of the sorted map: "+tsmap);

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

Typesafe view of the sorted map: {1=Only, 2=tutorials, 3=point}