Unmodifiable List in Java
This post will discuss various methods to create an unmodifiable list in Java.
Unmodifiable lists are “read-only” wrappers over other collections. They do not support any modification operations such as add, remove, and clear, but their underlying collection can be changed. Lists that additionally guarantee that no change in the Collection object will ever be visible are referred to as immutable.
It is worth noting that making a list final will not make it Unmodifiable. We can still add elements or remove elements from it. Only the reference to the list is final.
1. Using Arrays.asList() method
Arrays.asList() method returns a fixed-size list backed by the specified array. Since an array cannot be structurally modified, it is impossible to add elements to the list or remove elements from it. The list will throw an UnsupportedOperationException if any resize operation is performed on it.
However, the list can be modified if:
set(int index, E element)is called on the list which will replace the element at the specified index in this list with the specified element.- Any change is made to the original array which will be reflected back in the returned list.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Arrays; import java.util.List; class Main { // Unmodifiable list in Java public static void main(String[] args) { String[] lang = new String[] { "C", "C++", "Java" }; // using plain Java List<String> fixedLengthList = Arrays.asList(lang); try { // any add or remove operation on the list will result in // an `UnsupportedOperationException` fixedLengthList.add("C#"); System.out.println("List : " + fixedLengthList); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // 1. the list can be modified by calling `set()` method fixedLengthList.set(1, "Go"); // 2. any changes made to the original array will be reflected // in the list lang[2] = "JS"; System.out.println("Array : " + Arrays.toString(lang)); } } |
Output:
java.lang.UnsupportedOperationException
List : [C, C++, Java]
Array : [C, Go, JS]
2. Using Collections.unmodifiableList() method
Collections unmodifiableList() method returns an unmodifiable “read-only” view of the specified list. Any attempt to modify the returned list directly or via its iterator will result in an UnsupportedOperationException. However, any changes made to the original list will be reflected in the unmodifiable list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { // Unmodifiable list in Java public static void main(String[] args) { List<String> mutableList = new ArrayList<>( Arrays.asList("C", "C++", "Java")); // Using Java Collections List<String> unmodifiableList = Collections.unmodifiableList(mutableList); try { // any attempt to modify the list will result in // an `UnsupportedOperationException` unmodifiableList.add("C#"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // any changes made to the original list will be reflected // back in the unmodifiable list mutableList.add("Go"); System.out.println(unmodifiableList); } } |
Output:
java.lang.UnsupportedOperationException
[C, C++, Java, Go]
3. Using Collections.unmodifiableCollection() method
Collection interface offers another method, unmodifiableCollection(), which returns an unmodifiable view of the specified collection. This is similar to unmodifiableList() except it returns a Collection instead of a List.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.*; class Main { // Unmodifiable list in Java public static void main(String[] args) { List<String> mutableList = new ArrayList<>( Arrays.asList("C", "C++", "Java")); // Using Java Collections Collection<String> unmodifiableList = Collections.unmodifiableCollection(mutableList); try { // any attempt to modify the collection will result in // an `UnsupportedOperationException` unmodifiableList.add("C#"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // any changes made to the original list will be reflected // back in the unmodifiable list mutableList.add("Go"); System.out.println(unmodifiableList); } } |
Output:
java.lang.UnsupportedOperationException
[C, C++, Java, Go]
4. Using Apache Commons Collections
Apache Commons Collections ListUtils class provides unmodifiableList() method that returns an unmodifiable list backed by the given list. If the given list is null, it throws a NullPointerException. The list will throw an UnsupportedOperationException if any modification operation is performed on it. However, any changes made to the original list will be reflected in the returned list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import org.apache.commons.collections4.ListUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { // Unmodifiable list in Java public static void main(String[] args) { String[] lang = new String[] { "C", "C++", "Java" }; List<String> mutableList = new ArrayList<>(Arrays.asList(lang)); // Apache Commons Collections – creates a view List<String> unmodifiableList = ListUtils.unmodifiableList(mutableList); try { // any attempt to modify the list will result in // an `UnsupportedOperationException` unmodifiableList.add("C#"); } catch (UnsupportedOperationException ex) { System.out.println("java.lang.UnsupportedOperationException"); } // modify the original list – changes will be reflected // back in the `unmodifiableList` mutableList.set(1, "Go"); // change C++ to Go mutableList.remove(2); // remove Java mutableList.add("Perl"); // add Perl System.out.println(unmodifiableList); } } |
Output:
java.lang.UnsupportedOperationException
[C, Go, Perl]
That’s all about unmodifiable List in Java.
Suggested Read:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)