Immutable List in Java

In recent posts, we have seen how to create an ArrayList in java. There are many ways to create a mutable ArrayList in java. In this tutorial, we will learn what is immutable List in Java and how to make an immutable list.

Here is the table content of the article will we will cover this topic.
1. What is an immutable List in Java?
2. Advantages/Need of immutable list
3. How does it work?
4. Creating Immutable List in Java
i) By use of copyOf() method We can create ImmutableList
ii) By using of() method of ImmutableList class we can create ImmutableList
ii) We can create ImmutableList by Using Of() method List Class in Java 9 Factory.
iv) We can create ImmutableList by Using Builder() method
5. The important concepts of Immutable List

What is an immutable List in Java?

As you know List provides some Classes like ArrayList that is growable Array that can increase or decrease the size itself when the modification is done by the user.
An immutable List is a type of List that is immutable. It means the immutable List is read-only. You can’t modify the content of the immutable List after declaration. So an immutable list is threaded safe.

Advantages/Need of immutable list

As you know, an immutable object will not change after the declaration. These objects are thread-safe by default. Because multiple threads can read them only, they can’t change the object’s state. The same logic applies to an immutable List.

Suppose we have an immutable List with some values. It means the object is immutable and carries some values. The immutable list is thread-safe because Making it immutable will create a “read-only” version of our set.

As we already discuss the immutable list is ready only. Because if the user tries to modify(add or remove from list ) the list then the compiler will throw UnsupportedOperationException. The most common use case of an immutable set is in a multi-threaded environment. Multiple threads can share immutable set data without worrying about synchronization.

How does it work?

In an immutable list, the user can modify the object properties but not the object.
Let’s say you have an immutable list that stores the 5 students’ records. Each student has a name, class, roll no, etc.
You can change the name of the student, class name, and roll no. But you can’t change the number of students in the immutable list (add or delete student).

Immutable List in Java
import java.util.List;

public class ImmutableListExample
{
	public static void main(String[] args) 
	{
		Student student1 = new Student(1, "MCA", "Ravi");
		Student student2 = new Student(2, "BCA", "Ram");
		Student student3 = new Student(3, "BCA", "Ravi");
		
		//creating list by using of() method
		List<Student> immutableList = List.of(student1, student2, student3); 
        
		for(Student s : immutableList)
		{
			System.out.println("Rollno :" + s.getRollNo());
			System.out.println("Class Name :" + s.getClassName());
			System.out.println("Name :" + s.getName());
		}
		
		// Changing the class Name of rollno 2
		immutableList.get(1).setClassName("MCA");
		
		System.out.println("After make chnges of Rollno 2: "+ immutableList.get(1).getClassName());   
	}
}

class Student
{
	int rollNo;
	String className;
	String name;
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Student(int rollNo, String className, String name) {
		this.rollNo = rollNo;
		this.className = className;
		this.name = name;
	}
}

Output: Rollno :1
Class Name :MCA
Name :Ravi
Rollno :2
Class Name :BCA
Name :Ram
Rollno :3
Class Name :BCA
Name :Ravi
After make chnges of Rollno 2: MCA

Creating Immutable List in Java

You can create an immutable list in various ways.

1. By use of copyOf() method We can create ImmutableList

ImmutableList<E> copyOf(Collection<? extends E> elements)

It returns an immutable list that contains all the elements of the given list/collection. It will throw NullPointerException if any element is null in the given collection.

import com.google.common.collect.ImmutableList; 
import java.util.*; 

class ExampleOfImmutableList
{ 
	public static void main(String[] args) 
	{
	    // Create Immutablelist using copyOf() 
	    ImmutableList<String> immutableList2 = 
						ImmutableList.copyOf(Arrays.asList("Learning", "Web", "Site")); 
		
		
	List<String> list = new ArrayList<String>(Arrays.asList("Java", "Goal", "com")); 
		
       // Create Immutablelist from existing list by using copyOf() 
		ImmutableList<String> immutableList1 = 
						ImmutableList.copyOf(list); 
      
      // Print the Immutablelist
		System.out.println(immutableList1); 
      // Print the Immutablelist
		System.out.println(immutableList2); 
	}
}

Output: [Java, Goal, com]
[Learning, Web, Site]

2. By using of() method of ImmutableList class we can create ImmutableList

ImmutableList<E> of() 

It returns an immutable list containing the given element.

import com.google.common.collect.ImmutableList; 
import java.util.*; 

class ExampleOfImmutableList
{ 

	public static void main(String[] args) 
	{
	    // Create Immutablelist using Of() method
		ImmutableList<String> immutableList = 
						ImmutableList.of("Learning", "Web", "Site", "Java", "Goal"); 
		
      // Print the Immutablelist
		System.out.println(immutableList); 
	}
}

Output:[Learning, Web, Site, Java, Goal]

3. We can create ImmutableList by Using Of() method List Class in Java 9 Factory.

Please Note: This program can run only in Java 9. You need a Java 9 compiler to run them.

import java.util.List; 

public class ImmutableListExample
{
	public static void main(String[] args) 
	{
		// non-empty immutable set 
        List<String> list = List.of(Learning", "Web", "Site", "Java", "Goal"); 
  
        // Let's print the list 
        System.out.println(list); }
}

Output: [Learning, Web, Site, Java, Goal]

4. We can create ImmutableList by Using Builder() method

By use of the Builder() method, We can create a new ImmutableList or an ImmutableList can be created from an existing List or both.

import com.google.common.collect.ImmutableList; 
import java.util.List;

class ImmutableListExample
{ 
	public static void main(String[] args) 
	{
        // creating immutable list   
		ImmutableList<String> newImmutableList = ImmutableList.<String>builder() 
										.add("Java", "Goal") 
										.build(); 
                                          
         List<String> existingImmutableList = List.of("Java", "Goal", "com"); 
// creating immutable list from existing       
 ImmutableList<String> list2 = ImmutableList.<String>builder() 
                                          .addAll(existingImmutableList) 
                                          .build(); 
                                          
        List<String> existingImmutableList2 = List.of("Java", "Goal", "com"); 
     // creating immutable listfrom existing and add more element  
 ImmutableList<String> list3 = ImmutableList.<String>builder() 
                                          .addAll(existingImmutableList2) 
                                          .add("Learning", "Website") 
                                          .build(); 
                                           
        // Let's print the List 
        System.out.println(newImmutableList);
        System.out.println(list2);
         System.out.println(list3);
	}
	
}

Output: [Java, Goal]
[Java, Goal, com]
[Java, Goal, com, Learning, Website]

The important concepts of Immutable List

If a user tries to modify the immutable List(add, delete and update elements in the List) then UnsupportedOperationException is thrown. Because you can’t modify an immutable List.

import java.util.List;

public class ImmutableListExample
{
	public static void main(String[] args) 
	{
		List<String> immutableList = List.of("Java", "Goal", "com"); 
        // Let's print the list 
        System.out.println(immutableList);
        
        //try to add the element in list
        immutableList.remove("Hello");
	}
}

Output: [Java, Goal, com]
Exception in thread “main” java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71) at
java.base/java.util.ImmutableCollections$AbstractImmutableCollection.remove(ImmutableCollections.java:78) at
Create.ImmutableListExample.main(ImmutableListExample.java:14)

An Immutable List does not allow any null element. If a user tries to add null in the Immutable List the compiler will throw UnsupportedOperationException.

import java.util.List;

public class ImmutableListExample
{
	public static void main(String[] args) 
	{
		List<String> immutableList = List.of("Java", "Goal", "com"); 
        // Let's print the list 
        System.out.println(immutableList);
        
        //try to add the element in list
        immutableList.add("Hello");
	}
}

Output: [Java, Goal, com]
Exception in thread “main” java.lang.UnsupportedOperationException at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:71) at java.base/java.util.ImmutableCollections$AbstractImmutableCollection.add(ImmutableCollections.java:75) at Create.ImmutableListExample.main(ImmutableListExample.java:14)

If a user tries to create an Immutable List with a null element, then the compiler will throw  NullPointerException.

import java.util.List;

public class ImmutableListExample
{
	public static void main(String[] args) 
	{
		List<String> immutableList = List.of("Java", "Goal", null); 
        // Let's print the list 
        System.out.println(immutableList);
	}
}

Output: Exception in thread “main” java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:221) at java.base/java.util.ImmutableCollections$ListN.<init>(ImmutableCollections.java:423) at java.base/java.util.List.of(List.java:842) at Create.ImmutableListExample.main(ImmutableListExample.java:9)

If the user is creating an ImmutableList from an existing List and change the existing List, then the changes don’t reflect on ImmutableList. Because we have created a copy of the existing list.

Leave a Comment