Java Programming Tutorials

Java programming tutorials with many code examples!

Java immutable List

Problem:

How to create an immutable List in Java? There are couple of ways to do that, one of them is to use Java Collections Framework as can be seen in the following example.

Solution:

In the example we use Collections.unmofidiableList(List) from java.util package and demonstrate that it cannot be modified – trying to do so results in UnsupportedOperationException:

package com.farenda.java;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class CollectionsUnmodifiableList {

    public static void main(String[] args) {
        List<Integer> arrayList = Arrays.asList(1, 2, 3);
        List<Integer> scores = new LinkedList<>(arrayList);

        System.out.println("scores: " + scores);

        arrayList.set(0, 7);
        System.out.println("updated array list: " + arrayList);

        scores.add(42);
        System.out.println("update scores: " + scores);

        scores = Collections.unmodifiableList(scores);

        try {
            scores.add(7);
        } catch (UnsupportedOperationException e) {
            System.out.println("While adding: " + e.getClass().getSimpleName());
        }

        try {
            scores.set(0, 7);
        } catch (UnsupportedOperationException e) {
            System.out.println("While setting: " + e.getClass().getSimpleName());
        }
    }
}

And here’s the result of running the example:

scores: [1, 2, 3]
updated array list: [7, 2, 3]
update scores: [1, 2, 3, 42]
While adding: UnsupportedOperationException
While setting: UnsupportedOperationException

The list returned from Arrays.asList(…) is fixed-size and also doesn’t allow to add new elements, but allows modifications (set operation) as you can see in the example. The List returned from Collections.unmodifiableList(List) doesn’t allow neither add nor sets and is truly immutable.

java.util.Collections has helper methods to create immutable versions of different collections:

  • <T> Collection<T> unmodifiableCollection(Collection<? extends T> c):
    Returns an unmodifiable view of the specified collection.
  • <T> List<T> unmodifiableList(List<? extends T> list):
    Returns an unmodifiable view of the specified list.
  • <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m):
    Returns an unmodifiable view of the specified map.
  • <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K,? extends V> m) (since: Java 1.8):
    Returns an unmodifiable view of the specified navigable map.
  • <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) (since: Java 1.8):
    Returns an unmodifiable view of the specified navigable set.
  • <T> Set<T> unmodifiableSet(Set<? extends T> s):
    Returns an unmodifiable view of the specified set.
  • <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m):
    Returns an unmodifiable view of the specified sorted map.
  • <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s):
    Returns an unmodifiable view of the specified sorted set.

Alternatively you can use ImmutableList, ImmutableSet, and ImmutableMap from Guava library.

Share with the World!