In this article, we will discuss how to convert a modifiable ArrayList into an unmodifiable ArrayList using Collections.unmodifiableList(); method
1. Convert ArrayList to Read-only:
- ArrayList is an implementation-class of List interface which grows/shrink in size dynamically whenever elements is
- added to an ArrayList using add() method
- deleted from existing ArrayList using remove() method
- modified/updated using set() method at particular position
- For some business purpose/requirement, ArrayList needs to be made read-only or immutable or unmodifiable
- To achieve this, use one of the utility method called unmodifiableList() from Collections class
- Syntax : Collections.unmodifiableList(modifiableList); i.e.; pass modifiable list as input-argument
- Trying to add/remove/modify/update an unmodifiable List throws UnsupportedOperationException as shown in the below example
- Note: however, adding/deleting/updating elements in the original ArrayList is still possible
ConvertArrayListToReadOnly.java
package net.bench.resources.unmodifiable.collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ConvertArrayListToReadOnly {
public static void main(String[] args) {
// 1. list of String
List<String> list = new ArrayList<>();
// 1.1 add names
list.add("Woman");
list.add("Batman");
list.add("Superman");
list.add("Spiderman");
list.add("Human");
// 1.2 iterating/printing original List
System.out.println("1. Orginal list : \n");
list.forEach(System.out::println);
// 2. convert modifiable list to immutable list
List<String> unmodifiableList = Collections
.unmodifiableList(list);
// 2.1 iterating/printing original List
System.out.println("\n\n2. Read-only list : \n");
unmodifiableList.forEach(System.out::println);
// 3. trying to modify unmodifiable list
System.out.println("\n\n3. Trying to modify unmodifiable list : \n");
try {
unmodifiableList.add("Newman");
}
catch(UnsupportedOperationException usopex) {
System.out.println("In Exception block : ");
usopex.printStackTrace();
}
}
}
Output:
1. Orginal list :
Woman
Batman
Superman
Spiderman
Human
2. Read-only list :
Woman
Batman
Superman
Spiderman
Human
3. Trying to modify unmodifiable list :
In Exception block :
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
at net.bench.resources.unmodifiable.collection.ConvertArrayListToReadOnly
.main(ConvertArrayListToReadOnly.java:41)
Related Articles:
- Java – How to make List or ArrayList unmodifiable List or read-only ?
- Java – How to make Set or HashSet unmodifiable Set or read-only ?
- Java – How to make Map or HashMap unmodifiable Map or read-only ?
- Java – How to make any Collection class as unmodifiable Collection or read-only ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-
- https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/8/docs/api/java/util/List.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Happy Coding !!
Happy Learning !!