Collections unmodifiableMap() method in Java with Examples Last Updated : 08 Oct, 2018 Comments Improve Suggest changes Like Article Like Report The unmodifiableMap() method of java.util.Collections class is used to return an unmodifiable view of the specified map. This method allows modules to provide users with "read-only" access to internal maps. Query operations on the returned map "read through" to the specified map, and attempts to modify the returned map, whether direct or via its collection views, result in an UnsupportedOperationException. The returned map will be serializable if the specified map is serializable Syntax: public static <K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m) Parameters: This method takes the map as a parameter for which an unmodifiable view is to be returned. Return Value: This method returns an unmodifiable view of the specified map. Below are the examples to illustrate the unmodifiableMap() method Example 1: Java // Java program to demonstrate // unmodifiableMap() method // for <String, String> value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Hashtable<String, String> Hashtable<String, String> table = new Hashtable<String, String>(); // populate the table table.put("key1", "1"); table.put("key2", "2"); table.put("key3", "3"); // getting unmodifiable map // using unmodifiableMap() method Map<String, String> m = Collections .unmodifiableMap(table); // printing the unmodifiableMap System.out.println("Initial collection: " + table); } catch (UnsupportedOperationException e) { System.out.println("Exception thrown : " + e); } } } Output: Initial collection: {key3=3, key2=2, key1=1} Example 2: For UnsupportedOperationException Java // Java program to demonstrate // unmodifiableMap() method // for <String, String> value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // creating object of Hashtable<String, String> Hashtable<String, String> table = new Hashtable<String, String>(); // populate the table table.put("key1", "1"); table.put("key2", "2"); table.put("key3", "3"); // getting unmodifiable map // using unmodifiableMap() method Map<String, String> m = Collections .unmodifiableMap(table); // printing the unmodifiableMap System.out.println("Initial collection: " + table); // Adding element to new Collection System.out.println("\nTrying to modify" + " the unmodifiablemap"); m.put("key4", "4"); } catch (UnsupportedOperationException e) { System.out.println("Exception thrown : " + e); } } } Output: Initial collection: {key3=3, key2=2, key1=1} Trying to modify the unmodifiablemap Exception thrown : java.lang.UnsupportedOperationException Comment More infoAdvertise with us Next Article Collections unmodifiableMap() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Advance Java Java-Collections Java - util package Java-Functions Practice Tags : Java-Collections Similar Reads Collections unmodifiableSortedMap() method in Java with Examples The unmodifiableSortedMap() method of java.util.Collections class is used to return an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specifi 2 min read Collections unmodifiableSet() method in Java with Examples The unmodifiableSet() method of java.util.Collections class is used to return an unmodifiable view of the specified set. This method allows modules to provide users with "read-only" access to internal sets. Query operations on the returned set "read through" to the specified set, and attempts to mod 2 min read Collections unmodifiableList() method in Java with Examples The unmodifiableList() method of java.util.Collections class is used to return an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts t 2 min read Java Collections unmodifiableNavigableMapâ() Method with Examples The unmodifiableMap() in java collections is used to get the unmodifiable view for the given map. Syntax: public static <Key,Value> Map<Key,Value> unmodifiableMap(Map<? extends Key, ? extends Key> map) Parameters: Key is the key-value typeValue is the value typemap is the input map 2 min read Collections unmodifiableCollection() method in Java with Examples The unmodifiableCollection() method of java.util.Collections class is used to return an unmodifiable view of the specified collection. This method allows modules to provide users with "read-only" access to internal collections. Query operations on the returned collection "read through" to the specif 2 min read Like