Java Collection Tutorial - Java Collections .unmodifiableSortedMap ( SortedMap <K,? extends V> m)








Syntax

Collections.unmodifiableSortedMap(SortedMap <K,? extends V> m) has the following syntax.

public static <K,V> SortedMap <K,V> unmodifiableSortedMap(SortedMap <K,? extends V> m)

Example

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

//  w  ww .j av  a2  s. c o m
import java.util.Collections;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class Main {
   public static void main(String[] s) {
      // create sorted map
      SortedMap<String,String>  map = new TreeMap<String, String> ();
      
      // populate the set
      map.put("1","New");
      map.put("2","to");
      map.put("3","java2s.com");
      
      System.out.println("Initial sorted map value: "+map);
      
      // create unmodifiable sorted map
      Map<String,String> unmodsortmap = Collections.unmodifiableSortedMap(map);

      // try to modify the sorted map
      unmodsortmap.put("4","Modify");
   }
}

The code above generates the following result.