This article will discuss different ways to create a mutable, unmodifiable, immutable, and fixed-length 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 we can modify their underlying list. Lists that 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

⮚ Using Plain Java

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

⮚ Using Guava

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

⮚ Java 8

We can use the Java 8 Stream 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

⮚ Using Collections

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

⮚ Using Apache Collections: ListUtils Class

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

 
Both the above methods throw 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 an immutable empty list in Java. The list will throw an UnsupportedOperationException if any modification operation is performed on it.

⮚ Using Arrays.asList()

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

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

⮚ Using Collections

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

 
The above method might throw an unchecked assignment warning. The following example demonstrates the type-safe way to obtain an empty list:

⮚ In Java 8

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

⮚ Using Guava

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

1. ImmutableList.copyOf returns an immutable empty list if the 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 create an immutable empty list instance similarly.

 
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 cast to any type as it will never hold any elements.

⮚ Using Apache Collections

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

⮚ In Java 9

Java 9 comes with static factory methods on the List interface that can create compact, unmodifiable instances. We can use the List.of() to create an immutable empty list. The list will throw an UnsupportedOperationException if any modification 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. We cannot add elements to the returned list, and it will throw an UnsupportedOperationException if any resize operation is performed on it.

That’s all about mutable, unmodifiable, and immutable empty List in Java.