Wednesday, August 4, 2021

How to Create Read Only List, Map and Set in Java? UnModifiable Collection Example

Read-only List, Map and Set in Java
A read-only List means a List where you can not perform modification operations like add, remove or set. You can only read from the List by using the get method or by using the Iterator of List, This kind of List is good for a certain requirement where parameters are final and can not be changed. In Java, you can use Collections.unModifiableList() method to create read-only List, Collections.unmodifiableSet() for creating read-only Set like read-only HashSet and similarly creating a read-only Map in Java, as shown in below example. Any modification in the read-only List will result in java.lang.UnSupportedOperationException in Java.  

This read-only List example is based on Java 5 generics but also applicable to other Java versions like JDK 1.4 or JDK 1.3, just remove Generics code i.e. angle bracket which is not supported prior to Java 5. One common mistake a programmer makes is that assuming fixed size List and read-only List as the same. 

As shown in our 3 examples of converting Array to Array List, we can use Arrays.asList() method to create and initialize List at the same line. List implementation returned by this method is a fixed size and it doesn’t allow adding or removal of elements but it is not read-only because you can update objects by calling the set(index) method. 

How to make a collection read-only is also a popular Java collection interview question, which makes this Java collection tutorial even more important.

Read-only List, Set, and Map Example - Java



Here is sample Java program which demonstrate method of creating read only List, Set and Map in Java. You can make any List, Set or Map implementation as read only by following code example. Just remember that Collections.unModifiableList() , Collections.unModifiableSet() and Collections.unModifiableMap() method in Java

Example to create read only List Set and Map in JavaOn read-only List add, remove and set operation is not permitted, on read only Set you can not add or remove elements and in read only Map you can not put new entries or update existing entries. You can use same methods to convert any List implementation like ArrayList, LinkedList or Vector to read only ArrayList, LinkedList and Vector in Java.

package example;

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;



/**
 * Java program to create read only List, Set and Map in Java. You can first create
 * a List or Set and than make it unmodifiable or read-only by
 * using  Collections.unmodifiableList() or Collections.unmodifiableSet() method.
 *
 * @author Javin Paul
 */

public class ReadOnlyListSetMap {
 
    public static void main(String args[]) {            
       
        // creating List in Java
        List<String> contents = new ArrayList<String>();
     
        // initializing List in Java
        contents.add("Example");
        contents.add("Tutorial");
        contents.add("Program");
     
     
        // Currently This List is not read only, you can add or remove elements from List
        contents.add("Tips"); //should not be allowed if List is read only.
     
        System.err.println("normal List in Java : " + contents);
     
        //creating readonly List from contents
        contents = Collections.unmodifiableList(contents);
     
        //java.lang.UnsupportedOperationException -- no modification in read only list
     
       //not allowed as it is read-only List in Java
       contents.add("Can I add object into read only List - No");

       
        contents.remove("Example"); //remove not allowed in read only list
     
        //java.lang.UnSupportedOperation - List update not allowed
        contents.set(0, "Can I override or set object in read-only Set - No");

     
        //Creating read only Set in Java
        //similar to read-only List you can also create a Set which is read only
        //i.e. addition , removal and modification operation is not permitted on list
     
     
        //Creating a Set based on contents of List      
        Set<String> readOnlySet = new HashSet<String>(contents);
     
        System.out.println("original Set in Java : " + readOnlySet);

        //Set is not yet read-only you can still add elements into Set
        readOnlySet.add("Override");

        System.out.println("Set before making read only : " + readOnlySet);
     
        //making Set readonly in Java - no add remove or set operation permitted
        readOnlySet = Collections.unmodifiableSet(readOnlySet);
     
        //trying to add element in read only Set - java.lang.UnSupportedOperationException
        readOnlySet.add("You can not add element in read Only Set");
     
        //trying to remove element from read only set
        readOnlySet.remove("Example"); //you can not remove elements from read only Set
     
     
     
        // Creating read only Map in Java
        // Similar to List and Set you can also create read only or unmodifiable Map in Java
        // add , remove and override is not allowed on read only Map in Java
     
        Map<String, String> contries = new HashMap<String, String>();      
        contries.put("India", "New Delhi");
     
        //Map is not read only yet, you can still add entries into
        contries.put("UK", "London");
     
        System.out.println("Map in Java before making read only: " + contries);
     
        //Making Map read only in Java
        Map readOnlyMap = Collections.unmodifiableMap(contries);
       
        //you can not put a new entry in read only Map in Java
        readOnlyMap.put("USA", "Washington"); //java.lang.UnSupportedOperation
       
        //you can not remove keys from read only Map in Java
        readOnlyMap.remove("UK"); //java.lang.UnSupportedOperation
     
    }
 
}


That’s all on how to create a read-only List, Set, and Map in Java. It's very easy to make any List implementation read-only by wrapping it with Collections.unModifiableList(). Use the same technique to convert any other Collection into read-only in Java.


Other Java collection tutorial from Javarevisited

3 comments:

  1. There is a limitation of Collections.unmodifiableList(myList) method, it will not work in case of nested lists. For example if your List contains reference to another list then this method will make outer most List unmodifiable, you can still get reference of nested list and can modify it. here is an example, which confirms that :

    import java.util.Collections;
    import java.util.List;
    import java.util.ArrayList;


    public class Sample {


    public static void main(String args[]){

    List outer = new ArrayList();
    outer.add("C++");
    outer.add("C");
    outer.add("Python");

    List inner = new ArrayList();
    inner.add("Java");

    outer.add(inner); // adding List to outer List

    // read only list
    List unmodifiable = Collections.unmodifiableList(outer);

    System.out.println("List before modification : " + unmodifiable);

    List myInner = (List) unmodifiable.get(3);
    myInner.add("JEE");
    myInner.add("Java ME");

    System.out.println("List after modification : " + unmodifiable);

    }


    }

    When you run this program it will print :
    List before modification : [C++, C, Python, [Java]]
    List after modification : [C++, C, Python, [Java, JEE, Java ME]]

    Which clearly shows that you can still modify inner List, which means unmodifiableList() method is only good if your List doesn't contain another List or Collection.

    ReplyDelete
  2. the innerlist which is also mutable object can be changed normally, there is nothing unexpected..

    ReplyDelete
  3. Now you can use List.of()/ Set.of()/Map.of() from java 9 which makes your collection immutable

    ReplyDelete