java.util.Collections.unmodifiableSortedMap() Method

Advertisements


Description

The unmodifiableSortedMap() method is used to return an unmodifiable view of the specified sorted map.

Declaration

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

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

Parameters

  • m--This is the map for which an unmodifiable view is to be returned.

Return Value

The method call returns an unmodifiable view of the specified sorted map.

Exception

  • NA

Example

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

package com.tutorialspoint;


import java.util.*;

public class CollectionsDemo {
   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","TP");
      
      System.out.println("Initial sorted map value: "+map);
      
      // create unmodifiable sorted map
      Map unmodsortmap = Collections.unmodifiableSortedMap(map);

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

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

Initial sorted map value: {1=New, 2=to, 3=TP}
Exception in thread "main" java.lang.UnsupportedOperationException



Advertisements
Advertisements