VOOZH about

URL: https://www.geeksforgeeks.org/java/immutable-list-in-java/

⇱ Immutable List in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Immutable List in Java

Last Updated : 11 Jul, 2025
  • ImmutableList, as suggested by the name, is a type of List which is immutable. It means that the content of the List are fixed or constant after declaration, that is, they are read-only.
  • If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown.
  • An ImmutableList does not allow null element either.
  • If any attempt made to create an ImmutableList with null element, NullPointerException is thrown. If any attempt is made to add null element in List, UnsupportedOperationException is thrown.
Advantages of ImmutableList
  • They are thread safe.
  • They are memory efficient.
  • Since they are immutable, hence they can be passed over to third party libraries without any problem.
Note that it is an immutable collection, not collection of immutable objects, so the objects inside it can be modified.
Class Declaration:
@GwtCompatible(serializable=true,
 emulated=true)
public abstract class ImmutableList
extends ImmutableCollection
implements List, RandomAccess
Class hierarchy:
java.lang.Object
 ↳ java.util.AbstractCollection
 ↳ com.google.common.collect.ImmutableCollection
 ↳ com.google.common.collect.ImmutableList 
Creating ImmutableList ImmutableList can be created by various methods. These include:
  1. From existing List using copyOf() function of Guava Output:
    [Geeks, For, Geeks]
    
  2. New ImmutableList using of() function from Guava Output:
    [Geeks, For, Geeks]
    
  3. Using Java 9 Factory Of() method In Java, use of() with Set, Map or List to create an Immutable List. Please Note: The programs below are of Java 9. Hence you would need a Java 9 compiler to run them. Output:
    [Geeks, For, Geeks]
    
  4. Using Builder() from ImmutableList In Guava, ImmnutableList class provides a function Builder(). Through this function, a new ImmutableList can be created, or an ImmutableList can be created from an existing List or both.
    • Creating a new ImmutableList Output:
      [Geeks, For, Geeks]
      
    • Creating an ImmutableList from existing List Output:
      [Geeks, For, Geeks]
      
    • Creating a new ImmutableList including the existing List Output:
      [Geeks, For, Geeks, Computer, Portal]
      

Try to change ImmutableList As mentioned earlier, the below program will throw UnsupportedOperationException. Output :
Exception in thread "main" java.lang.UnsupportedOperationException
 at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:218)
 at ImmutableListDemo.main(Main.java:16)
How is it different from Collections.unmodifiableList()? Collections.unmodifiableList creates a wrapper around the same existing List such that the wrapper cannot be used to modify it. However we can still change original List. Output:
[Geeks, For, Geeks]
If we create an ImmutableList from an existing List and change the existing List, the ImmutableList does not change because a copy is created.
Output :
[Geeks]
Comment