unmodifiable な Map を作成する

こういうの欲しかったので...書き残し

記事

www.tutorialspoint.com

引用ソースコードと実行結果

import java.util.*;

public class CollectionsDemo {
   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", "value3");
      
   System.out.println("Initial collection: "+table);
      
   // create unmodifiable map
   Map m = Collections.unmodifiableMap(table);

   // try to modify the collection
   m.put("key3", "value3");
   }
}
Initial collection: {key3=value3, key2=value2, key1=value1}
Exception in thread "main" java.lang.UnsupportedOperationException