Java Collections unmodifiableMap() MethodThe unmodifiableMap() method of Java Collections class is used to get an unmodifiable view of the specified map. SyntaxFollowing is the declaration of unmodifiableMap() method: Parameter
ReturnsThe unmodifiableMap() method returns an unmodifiable view of the specified map. ExceptionsNA Example 1Test it NowOutput: Initial collection: {key1=Java, key2=Android, key3=JavaScript} Unmodifiable Map: {key1=Java, key2=Android, key3=JavaScript} Unmodifiable Map after adding: {key1=Java, key2=Android, key3=JavaScript, key4=JavaTpoint} Example 2Test it NowOutput: Initial Unmodifiable Map: {1=one, 2=two} Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.Collections$UnmodifiableMap.put(Collections.java:1453) at myPackage.CollectionsUnmodifiableMapExample2.main(CollectionsUnmodifiableMapExample2.java:10) Example 3Test it NowOutput: Unmodifiable map: {1=1001, 2=1002, 3=1003, 4=1004, 5=1005} Unmodifiable map after remove(4, 1004): {1=1001, 2=1002, 3=1003, 5=1005} Next TopicJava Collections Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India