← All posts
Julien Delange Tuesday, September 6, 2022

Good Practices for Java Immutable Lists

Share

AUTHOR

Julien Delange, Founder and CEO

Julien is the CEO of Codiga. Before starting Codiga, Julien was a software engineer at Twitter and Amazon Web Services.

Julien has a PhD in computer science from Universite Pierre et Marie Curie in Paris, France.

See all articles

Java is one of the most popular programming languages: according to the TIOBE index, it ranks at the third position of the most used programming languages.

Java started with an object-oriented approach first, where the user defines class and instantiates objects. The Java runtime offers a rich library developers can reuse. Still, at this time, functional programming and immutability were not as popular as today. And the Java ecosystem lacked support for immutable values.

Why Immutability

Using immutable values is a core tenet of the functional programming paradigm. One core principle of functional programming is to use pure functions. A pure function is a function where the output only depends on the input and the inputs are not mutated.

Using only pure functions helps you avoid side effects. You know that your input parameters are never modified.

The issue with the original List implementations

When Java was initially released, the List interface had few implementations. The commonly used implementation was ArrayList and we typically used the ArrayList as this:

1myList.stream().toArray(ElementClass[]::new);
2
List to array

hosted with ❤ by Codiga

The problem with ArrayList is that it's mutable and we can change it. Passing an immutable list is dangerous since the function may attempt to modify the list content. Making it immutable guarantees you that the list is not being modified. And even if the function attempts to modify the list, an UnsupportedOperationException will be thrown.

How to create immutable List instances?

List.of() static method (Java 9 and further)

Java 9 introduced the List.of() method to create an immutable list based on a list of objects. If you want to create a list of object that contains object1 and object2, use the following statement:

1List listname = List.of(elements);
2
Immutable list of elements

hosted with ❤ by Codiga

List.copyOf(existingCollection) static method (Java 9 and further)

Java 9 also introduced a capability to create a list from an existing collection. If you created a mutable list (e.g. an ArrayList) and want to make it immutable, you can generate an immutable list like this:

1List newlist = List.copyOf(existingList);
2

The case of the Google Guava library

Another way to create immutable lists is to use the ImmmutableList from the Guava library.

You can create an immutable list from an existing list of collections like this:

1newList = ImmutableList.copyOf(originalList);
2
Guava Immutable List

hosted with ❤ by Codiga

If you want to create a new immutable list from multiple existing lists, you can use the integrated builder. Let's say you want to create a new list newList from list1 and list2, you can do it easily using the builder:

1List newList = ImmutableList.builder().addAll(list1).addAll(list2).build();
2
Immutable list builder - Guava

hosted with ❤ by Codiga

Guava also has the ability to build an immutable list from elements but the value is limited since the List.of() method from the core Java library already includes this functionality.

1List newList = ImmutableList.of(object1, object2);
2
Immutable list from objects - Guava

hosted with ❤ by Codiga

Conclusion

Following functional programming principles and making your values immutable makes your code simpler. Even if the earlier versions of Java did not make it easy to create an immutable list, further versions introduced new functions to create an immutable list easily.

Suppose the functions from the standard library are not enough for you. In that case, you can use the Guava library that offers even more functionalities, such as creating an immutable list from multiple other lists.

Schedule a demo

Code analyzed in seconds with Codiga Automated Code Reviews.

Write code faster with the Codiga Coding Assistant.

Let's talk!