Immutable List in Java

In this post, we will discuss various methods to create immutable list in Java.


 

Unmodifiable Lists are “read-only” wrappers over other collections. They do not support any modification operations such as add, remove and clear but their underlying collection can be changed. Lists that additionally guarantee that no change in the Collection object will ever be visible are referred to as immutable.

When you don’t expect to modify a collection, it’s a good practice to defensively copy it into an immutable collection. Immutable Lists have many advantages over their mutable siblings, like they are thread-safe, more memory-efficient and can be passed to untrusted libraries without any side effects.

The Collections framework provides unmodifiableList() method, but it is unsafe to use as the returned list is only truly immutable if nobody holds a reference to the original collection. The returned list is also inefficient as the data structures will still have all the overhead of mutable collections, including concurrent modification checks, extra space in hash tables, etc.

 

1. Guava

Guava provides simple, easy-to-use immutable versions of each standard Collection type, including Guava’s own Collection variations. Unlike Collections’s unmodifiableList(), which is a view of a separate collection that can still change, an instance of ImmutableList contains its own private data and will never change.

 
An ImmutableList collection can be created in several ways:
 

1.1. Using the copyOf() method

ImmutableList.copyOf returns an immutable list containing the elements of the specified list. It returns a NullPointerException if any of elements is null.

Java

Download   Run Code

Output:

java.lang.UnsupportedOperationException
[C, C++, Java]

1.2. Using a Builder

Guava also provides a builder for creating immutable list instances as shown below:

Java

Download   Run Code

Output:

java.lang.UnsupportedOperationException
[C, C++, Java]

1.3. Using the of method

ImmutableList.of() returns an immutable list containing the given elements, in order.

Java

Download   Run Code

Output:

java.lang.UnsupportedOperationException
[C, C++, Java]

 

2. Java 9 – Collection factory methods

Several collection factory methods have been added in Java 9 that allow for easy initialization of immutable collections with specified elements. These factory methods are only available for List, Set, and Map Interfaces and the returned collection is structurally immutable. i.e. elements cannot be added, removed, or replaced from the collection. Any modification operation will throw an UnsupportedOperationException. However, if the contained elements are themselves mutable, this may cause the Colection’s contents to appear to change.

Java

Output:

java.lang.UnsupportedOperationException
[C, C++, Java]

 
References: Guava’s Wiki – Immutable Collections Explained

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz