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.
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
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:
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.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
object2, use the following statement:
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:
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
list2, you can do it easily using the builder:
1List newList = ImmutableList.builder().addAll(list1).addAll(list2).build(); 2
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
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.