Learnitweb

How to make an ArrayList read only in Java?

Sometimes there is a need to create a list which can not be modified. That is, list should not allow add, remove or set operations. Collections class provides unmodifiableCollection(Collection c) method to create unmodifiable collection. There are separate methods like unmodifiableList(List l) and unmodifiableSet(Set s) for creating unmodifiable list and set.

One of the misconception to create read only ArrayList is to use Arrays.asList(T... a) method. But this method returns a fixed-size list backed by the specified array. We can not add or remove elements but set operation is allowed.

Example 1

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

public class ReadOnlyListExample {
	public static void main(String args[]) {
		List<String> list = new ArrayList();
		list.add("one");
		list.add("two");
		list.add("three");
		
		System.out.println("Original list : " + list);
		List<String> unmodifiable = Collections.unmodifiableList(list);
		System.out.println("unmodifiable :" + list);
		try {
			unmodifiable.set(0, "x");
		}catch(Exception e) {
			System.out.println("set operation is not allowed");
			e.printStackTrace();
		}
		
		try {
			unmodifiable.remove("three");
		}catch(Exception e) {
			System.out.println("remove operation is not allowed");
			e.printStackTrace();
		}
		try {
			unmodifiable.add("four");
		}catch(Exception e) {
			System.out.println("add operation is not allowed");
			e.printStackTrace();
		}
	}
}

Output

Original list : [one, two, three]
unmodifiable :[one, two, three]
set operation is not allowed
java.lang.UnsupportedOperationException
  at java.util.Collections$UnmodifiableList.set(Collections.java:1311)
  at com.testdb.ReadOnlyListExample.main(ReadOnlyListExample.java:19)
remove operation is not allowed
java.lang.UnsupportedOperationException
  at java.util.Collections$UnmodifiableCollection.remove(Collections.java:1058)
  at com.testdb.ReadOnlyListExample.main(ReadOnlyListExample.java:26)
add operation is not allowed
java.lang.UnsupportedOperationException
  at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
  at com.testdb.ReadOnlyListExample.main(ReadOnlyListExample.java:32)

Example 2

import java.util.Arrays;
import java.util.List;

public class FixedLengthListExample {
	public static void main(String[] args) {
		List fixedLengthList = Arrays.asList("one", "two");
		try {
			fixedLengthList.add("four");
			System.out.println("add function is allowed");
		} catch (Exception e) {
			System.out.println("add is not allowed");
			e.printStackTrace();
		}

		try {
			fixedLengthList.remove("one");
			System.out.println("remove function is allowed");
		} catch (Exception e) {
			System.out.println("remove is not allowed");
			e.printStackTrace();
		}

		try {
			fixedLengthList.set(0, "x");
			System.out.println("set is allowed");
		} catch (Exception e) {
			System.out.println("set is not allowed");
			e.printStackTrace();
		}

	}
}

Output

add is not allowed
java.lang.UnsupportedOperationException
  at java.util.AbstractList.add(AbstractList.java:148)
  at java.util.AbstractList.add(AbstractList.java:108)
  at com.testdb.FixedLengthListExample.main(FixedLengthListExample.java:10)
remove is not allowed
java.lang.UnsupportedOperationException
  at java.util.AbstractList.remove(AbstractList.java:161)
  at java.util.AbstractList$Itr.remove(AbstractList.java:374)
  at java.util.AbstractCollection.remove(AbstractCollection.java:293)
  at com.testdb.FixedLengthListExample.main(FixedLengthListExample.java:18)
set is allowed