Mutable, Unmodifiable and Immutable Empty List in Java

In this article, we will discuss different ways to create mutable, unmodifiable and immutable empty list in Java.


 

Mutable Lists supports modification operations such as add, remove and clear on it. Unmodifiable Lists are “read-only” wrappers over other lists. They do not support add, remove and clear operations but their underlying list can be modified. Lists that additionally guarantee that no change in the list will ever be visible (even if their underlying list is modified) are referred to as Immutable.

It is worth noting that making a List final will not make it unmodifiable or immutable. We can still add elements or remove elements from it. Only the reference to the List is final.

 

1. Mutable Empty List

 

1.1. Using Plain Java –

We can simply use of the ArrayList constructor which constructs an empty resizable-array implementation of the List interface. Below code takes advantage of the new “diamond” syntax introduced in Java SE 7.

 

1.2. Using Guava –

Guava’s Lists.newArrayList() creates a creates a mutable, empty ArrayList instance. This method is deprecated in Java 7 and above. We should call ArrayList constructor directly.

 

1.3. Java 8 –

The Java 8 Stream API can be used to construct empty collections, by combining stream factory methods and collectors. Collectors.toList() returns a Collector that accumulates the input elements into a new List. To create an empty list, we can pass an empty array.

 

2. Unmodifiable Empty List

 

2.1. Using Collections –

Collections unmodifiableList() returns an unmodifiable view of the specified empty list.

 

2.2. Using Apache Collections: ListUtils Class –

Apache Commons Collections ListUtils.unmodifiableList() returns an unmodifiable list backed by the specified empty list.

 
Both above methods throws a NullPointerException if the specified list is null. If we try to modify the returned list, the list will throw an UnsupportedOperationException. However, any changes in the original mutable list will be visible in the unmodifiable list.

 

3. Immutable Empty List

There are several ways to create immutable empty List in Java. The List will throw an UnsupportedOperationException if any modify operation is performed on it.

 

3.1. Using Arrays.asList() –

Arrays.asList() method returns a fixed-size list backed by the specified array. If we don’t specify any array, this will return an immutable empty list.

The list will throw an UnsupportedOperationException if add or remove operation is performed on it and an ArrayIndexOutOfBoundsException if any update operation is performed on it (for instance, via the List.set(int, Object) method).

 

3.2. Using Collections –

We can use Collections.EMPTY_LIST that returns serializable and immutable empty list.

Above method might throw unchecked assignment warning. Below example illustrates the type-safe way to obtain an empty list:

 

3.3. In Java 8 –

We could adapt the Collectors.toList() collector discussed earlier to always produce an immutable empty list as shown below:

 

3.4. Using Guava –

Guava provides several static utility methods that can be used to obtain immutable empty list.

1. ImmutableList.copyOf returns an immutable empty list if specified list is empty.

The method will throw a NullPointerException if the specified list is null and any changes in the underlying mutable list will not be visible in the immutable list.

2. Guava also provides a builder that can be used to create immutable empty list instance in similar way.

 
3. ImmutableList.of() can also be used to return an immutable empty list.

This list behaves and performs comparably to Collections.emptyList(), and is preferable mainly for consistency and maintainability of code. The list is internally casted to any type as it will never hold any elements.

 

3.5. Using Apache Collections –

Apache Commons Collections provides ListUtils.EMPTY_LIST that returns immutable empty list.

 

3.6. In Java 9 –

Java 9 comes with static factory methods on the List interface that can creates compact, unmodifiable instances of it. We can use List.of() to create immutable empty list. The List will throw an UnsupportedOperationException if any modify operation is performed on it.

 

4. Fixed Sized Empty List

There is another type of empty list possible in Java apart from mutable, unmodifiable and immutable lists called fixed-sized list.

Apache Commons Collections ListUtils provides fixedSizeList() method that can return a fixed-sized empty list backed by the specified empty list. Elements cannot be added to the returned list and it will throw an UnsupportedOperationException if any resize operation is performed on it.

 
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