Open In App

Collections unmodifiableMap() method in Java with Examples

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 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 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

Article Tags :