Read Only Map in Java

In this example, We will show you how to make read only map in java.

Example Program

package com.dineshkrish;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
* @author Dinesh Krishnan
public class ReadOnlyMap {
public static void main(String[] args) {
// defining map object
Map<Integer, String> map = new HashMap<Integer, String>();
// adding the elements to map
map.put(1, "One");
map.put(2, "Two");
map.put(3, "Three");
map.put(4, "Four");
map.put(5, "Five");
// making read only map
map = Collections.unmodifiableMap(map);
// trying to update the map obejct
map.put(6, "One");

Run it

[su_highlight]Exception in thread “main” java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableMap.put(Unknown Source)
at com.dineshkrish.ReadOnlyMap.main(


1. Java List Interface
2. Java ArrayList Class
3. Java Collections Class
4. Java Collections.unmodifiableMap() Method


No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *