How to Create Unmodifiable List or Set or Map

How to Create Unmodifiable List or Set or Map

How to Create Unmodifiable List or Set or Map

In Java, Collections is one of the most popular and widely used frameworks in industry. Also all java developers will use List, Set and Map collections. Today we will see How to Create Unmodifiable List or Set or Map along with some examples. What will happen if we modify the read only collections? We will cover everything today.

Consider we have a list of Indian States, and the user is not able to add, delete or change the values in that list. The user can only able to read it. How can we achieve it?

What is Collections.unmodifiableList, Collections.unmodifiableSet, Collections.unmodifiableMap?

CommonUtil.java

package com.learnfromexamples.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CommonUtil {

    public static final List<String> indianStates = new ArrayList<String>();
    private static final Map<String,String> indianStateswithCodes = new HashMap<String,String>();
    static{
        indianStates.add("Andhra Pradesh");
        indianStates.add("Arunachal Pradesh");
        indianStates.add("Assam");
        indianStates.add("Bihar");
        indianStates.add("Chhattisgarh");
        indianStates.add("Goa");
        indianStates.add("Gujarat");
        indianStates.add("Haryana");
        indianStates.add("Himachal Pradesh");
        indianStates.add("Jammu and Kashmir");
        indianStates.add("Jharkhand");
        indianStates.add("Karnataka");
        indianStates.add("Kerala");
        indianStates.add("Madhya Pradesh");
        indianStates.add("Maharashtra");
        indianStates.add("Manipur");
        indianStates.add("Meghalaya");
        indianStates.add("Mizoram");
        indianStates.add("Nagaland");
        indianStates.add("Orissa");
        indianStates.add("Punjab");
        indianStates.add("Rajasthan");
        indianStates.add("Sikkim");
        indianStates.add("Tamil Nadu");
        indianStates.add("Telangana");
        indianStates.add("Tripura");
        indianStates.add("Uttar Pradesh");
        indianStates.add("Uttarakhand");
        indianStates.add("West Bengal");
    }
    
    static{
        indianStateswithCodes.put("AP","Andhra Pradesh");
        indianStateswithCodes.put("AR","Arunachal Pradesh");
        indianStateswithCodes.put("AS","Assam");
        indianStateswithCodes.put("BR","Bihar");
        indianStateswithCodes.put("CT","Chhattisgarh");
        indianStateswithCodes.put("GA","Goa");
        indianStateswithCodes.put("GJ","Gujarat");
        indianStateswithCodes.put("HR","Haryana");
        indianStateswithCodes.put("HP","Himachal Pradesh");
        indianStateswithCodes.put("JK","Jammu and Kashmir");
        indianStateswithCodes.put("JH","Jharkhand");
        indianStateswithCodes.put("KA","Karnataka");
        indianStateswithCodes.put("KL","Kerala");
        indianStateswithCodes.put("MP","Madhya Pradesh");
        indianStateswithCodes.put("MH","Maharashtra");
        indianStateswithCodes.put("MN","Manipur");
        indianStateswithCodes.put("ML","Meghalaya");
        indianStateswithCodes.put("MZ","Mizoram");
        indianStateswithCodes.put("NL","Nagaland");
        indianStateswithCodes.put("OR","Orissa");
        indianStateswithCodes.put("PB","Punjab");
        indianStateswithCodes.put("RJ","Rajasthan");
        indianStateswithCodes.put("SK","Sikkim");
        indianStateswithCodes.put("TN","Tamil Nadu");
        indianStateswithCodes.put("TG","Telangana");
        indianStateswithCodes.put("TR","Tripura");
        indianStateswithCodes.put("UP","Uttar Pradesh");
        indianStateswithCodes.put("UT","Uttarakhand");
        indianStateswithCodes.put("WB","West Bengal");
    }
    public static List<String> getIndianstates() {
        return Collections.unmodifiableList(indianStates);
    }
    
    public static Set<String> getIndianstates1() {
        return Collections.unmodifiableSet(new HashSet<String>(indianStates));
    }

    public static Map<String, String> getIndianstateswithcodes() {
        return Collections.unmodifiableMap(indianStateswithCodes);
    }
    
}

UnModifiableList.java

package com.learnfromexamples.readonlylist;

import com.learnfromexamples.util.CommonUtil;

public class UnModifiableList {
    public static void main(String[] args) {
        try{
            CommonUtil.getIndianstates().add("Pondicherryy");
        }
        catch(UnsupportedOperationException UOE){
            UOE.printStackTrace();
        }
        try{
            CommonUtil.getIndianstates1().add("Pondicherryy");
        }
        catch(UnsupportedOperationException UOE){
            UOE.printStackTrace();
        }
        try{
            CommonUtil.getIndianstateswithcodes().put("PU", "Pondicherry");
        }
        catch(UnsupportedOperationException UOE){
            UOE.printStackTrace();
        }
        
    }    
}

java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
    at com.learnfromexamples.readonlylist.UnModifiableList.main(UnModifiableList.java:8)
java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Unknown Source)
    at com.learnfromexamples.readonlylist.UnModifiableList.main(UnModifiableList.java:14)
java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableMap.put(Unknown Source)
    at com.learnfromexamples.readonlylist.UnModifiableList.main(UnModifiableList.java:20)

In the above example, We have two classes, one is Util class in which I have all hardcoded value and static methods to et the List, Set and Map. If we notify getIndianstates, getIndianstates1, getIndianstateswithcodes we have called Collections utility method i.e Collections.unmodifiableList, Collections. unmodifiableSet, Collections. unmodifiableMap which will prevent the user to perform any modification on List, Set and Map.

 UnsupportedOperationException

Now we prevented our Collections by performing the edit operations but what will happen if the user tries to modify Unmodified List, Set or Map? During run time it will throw UnsupportedOperationException exception

 

We have seen How to Create Unmodifiable List or Set or Map in this session to read more about collections use below links

Read:Comparing Interger Wrapper class with Equals and ==

Read:Comparing String Values with Equals and ==

Read:Comparing User Defined class with equals and ==

Read:Sorting Integer in ArrayList – Ascending and Descending order

Read:Sorting Strings in ArrayList – Ascending and Descending order

Read:Sorting Strings in ArrayList – Ascending and Descending order