Tuesday, July 31, 2012

How to create read only List, Map and Set in Java – unmodifiable 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 get method or by using 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 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 programmer makes is that assuming fixed size List and read only List as same. 

As shown in our 3 example of converting Array to Array List , we can use Arrays.asList() method to create and initialized List at same line. List implementation returned by this method is fixed size and it doesn’t allow adding or removal of element but it is not read only because you can update objects by calling 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
        // 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

        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 read only List, Set and Map in Java. Its very easy to make any List implementation read only by wrapping it with Collections.unModifiableList(). Use same technique to convert any other Collection into read only in Java.

Further Reading
Java Fundamentals, Part 1 and 2
Java Programming Interview Exposed
Java Fundamentals: Collections

Other Java collection tutorial from Javarevisited


Anonymous said...

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();

List inner = new ArrayList();

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("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.

erdemtopdas said...

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

Post a Comment