Home  >  Java 9

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.

List.of

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.
ListOf1.java
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.
imtList.add("DD"); 
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.
ListOf2.java
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);
	System.out.println(imtFinalList);
  }
} 

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.
ListOf3.java
import java.util.ArrayList;
import java.util.List;
public class ListOf3 {
  public static void main(String[] args) {
	List<String> list1 = new ArrayList<>();
	list1.add("A1");
	list1.add("B1");
	list1.add("C1");
	List<String> list2 = new ArrayList<>();
	list2.add("A2");
	list2.add("B2");
	list2.add("C2");	
	
	List<List<String>> finalList = List.of(list1, list2);
	System.out.println(finalList);
	
	list1.add("D2");
	System.out.println(finalList);
  }
} 
Output
[[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.
ListOf4.java
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) {
      this.id = id;
      this.name = 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.
UnmodifiableListTest.java
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.add("A1");
	list.add("B1");
	list.add("C1");
	
	List<String> unmodList = Collections.unmodifiableList(list);
	System.out.println(unmodList);
	
	list.add("D1");
	System.out.println(unmodList);
  }
} 
Output
[A1, B1, C1]
[A1, B1, C1, D1] 

References

Creating Immutable Lists
Java doc: List
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2020 concretepage.com | Privacy Policy | Contact Us