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








Syntax

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

public static <K,V> Map <K,V> unmodifiableMap(Map <? extends K,? extends V> m)

Example

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

import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;
/*from   ww  w. j  a  v a  2s. c  o m*/
public class Main {
   public static void main(String[] s) {
      //object hash table 
      Hashtable<String,String>  table = new Hashtable<String,String> ();
      
      // populate the table
      table.put("key1", "value1");
      table.put("key2", "value2");
      table.put("key3", "from java2s.com");
      
      System.out.println("Initial collection: "+table);
      
      // create unmodifiable map
      Map<String,String> m = Collections.unmodifiableMap(table);

      // try to modify the collection
      m.put("key3", "value3");
   }
}

The code above generates the following result.