Collections unmodifiableMap() method in Java with Examples Last Updated : 08 Oct, 2018 Summarize Comments Improve Suggest changes Share 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 in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read HashMap in Java In Java, HashMap is part of the Java Collections Framework and is found in the java.util package. It provides the basic implementation of the Map interface in Java. HashMap stores data in (key, value) pairs. Each key is associated with a value, and you can access the value by using the corresponding 15+ min read Stream In Java Stream was introduced in Java 8, the Stream API is used to process collections of objects. A stream in Java is a sequence of objects that supports various methods that can be pipelined to produce the desired result. Use of Stream in JavaThe uses of Stream in Java are mentioned below:Stream API is a 7 min read ArrayList in Java Java ArrayList is a part of the collections framework and it is a class of java.util package. It provides us with dynamic-sized arrays in Java. The main advantage of ArrayList is that, unlike normal arrays, we don't need to mention the size when creating ArrayList. It automatically adjusts its capac 9 min read Java List Interface The List Interface in Java extends the Collection Interface and is a part of the java.util package. It is used to store the ordered collections of elements. In a Java List, we can organize and manage the data sequentially. Key Features:Maintained the order of elements in which they are added.Allows 15+ min read Spring Tutorial Spring Framework is a comprehensive and versatile platform for enterprise Java development. It is known for its Inversion of Control (IoC) and Dependency Injection (DI) capabilities that simplify creating modular and testable applications. Key features include Spring MVC for web development, Spring 13 min read Stack Class in Java The Java Collection framework provides a Stack class, which implements a Stack data structure. The class is based on the basic principle of LIFO (last-in-first-out). Besides the basic push and pop operations, the class also provides three more functions, such as empty, search, and peek. The Stack cl 11 min read LinkedList in Java Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure, which is a linear data structure where the elements are not stored in contiguous locations, and every element is a separate object with a data part and an 12 min read Java Exercises - Basic to Advanced Java Practice Programs with Solutions Looking for Java exercises to test your Java skills, then explore our topic-wise Java practice exercises? Here you will get 25 plus practice problems that help to upscale your Java skills. As we know Java is one of the most popular languages because of its robust and secure nature. But, programmers 7 min read Like