Java Immutable List using List.of()

By Arvind Rai, January 30, 2020
We will create here Java immutable List using List.of() introduced in Java 9. Let us understand the immutable List and its usability.
1. An immutable List does not change its state after its construction. We cannot add, update or remove elements in an immutable List. An immutable List is completely immutable only if its containing objects are also immutable.
2. The List created by List.of() cannot add or remove any element and also cannot update object reference at any index. But if the containing objects of the List are not immutable then these objects can change its value.
3. The immutable List containing immutable objects are automatically thread safe. Immutable List consumes much less memory than the mutable one. If immutable List is containing mutable objects then that List is neither immutable not thread safe.
4. When an immutable List is performed with add, update or delete operation, it throws UnsupportedOperationException.
5. The usability of immutable List is when we create List with known values and that never changes. The immutable List increases the performance and saves memory, so even if List is subject to change occasionally, we can use immutable List to get those advantages.
6. The List returned by List.of() and Collections.unmodifiableList() are not the same. The List retuned by Collections.unmodifiableList() is a wrapper on source List and if source List goes to change, the unmodifiable List will also change.


Java List.of have following overloading.
static <E> List<E> of()
static <E> List<E> of(E e1)
static <E> List<E> of(E... elements)
static <E> List<E> of(E e1, E e2)
static <E> List<E> of(E e1, E e2, E e3)
static <E> List<E> of(E e1, E e2, E e3, E e4)
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5)
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6)
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7)
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)
static <E> List<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) 
The List.of() creates immutable List with zero elements. The List.of(E... elements) accepts arbitrary number of elements.

Creating Immutable List using List.of

Find the examples to create immutable List using List.of method.
Example-1: Find immutable List with string object.
import java.util.List;
public class ListOf1 {
  public static void main(String[] args) {
	List<String> imtList = List.of("AA", "BB", "CC");
	imtList.forEach(s -> System.out.println(s));
If we try to add element in immutable List, we will get error.
The error will be as following.
Exception in thread "main" java.lang.UnsupportedOperationException 
String objects are immutable objects and hence the List obtained from List.of method containing string objects is immutable.
Example-2: Find immutable List with immutable List objects.
import java.util.List;
public class ListOf2 {
  public static void main(String[] args) {
	List<String> imtList1 = List.of("A1", "B1", "C1");
	List<String> imtList2 = List.of("A2", "B2", "C2");	
	List<List<String>> imtFinalList = List.of(imtList1, imtList2);

Now look into another example where we create an immutable List with the objects of mutable List. We will see that when we change the mutable List value, there will also be change in immutable List values. So an immutable List containing the mutable objects is not the proper immutable List.
import java.util.ArrayList;
import java.util.List;
public class ListOf3 {
  public static void main(String[] args) {
	List<String> list1 = new ArrayList<>();
	List<String> list2 = new ArrayList<>();
	List<List<String>> finalList = List.of(list1, list2);
[[A1, B1, C1], [A2, B2, C2]]
[[A1, B1, C1, D2], [A2, B2, C2]] 

Example-3: Find the immutable List containing objects of custom immutable class.
import java.util.List;
public class ListOf4 {
  public static void main(String[] args) {
	Student s1 = new Student(101, "AAAA");
	Student s2 = new Student(101, "BBBB");
	Student s3 = new Student(101, "CCCC");
	List<Student> imtList = List.of(s1, s2, s3);
	imtList.forEach(s -> System.out.println(s.getName()));

final class Student {
  final private int id;  
  final private String name;
  public Student(final int id, final String name) { = id; = name;
  public int getId() {
    return id;
  public String getName() {
    return name;

List.of() vs Collections.unmodifiableList()

The List returned by List.of() and Collections.unmodifiableList() both are unmodifiable but there is difference that Collections.unmodifiableList() returns an unmodifiable view of the specified List. If we perform add, update or delete operation on source List specified to Collections.unmodifiableList() then the unmodifiable List returned by this method will also change.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class UnmodifiableListTest {
  public static void main(String[] args) {
	List<String> list = new ArrayList<>();
	List<String> unmodList = Collections.unmodifiableList(list);
[A1, B1, C1]
[A1, B1, C1, D1] 


Creating Immutable Lists
Java doc: List

©2024 | Privacy Policy | Contact Us