Immutable List in Java
In Java, an immutable list is a list that cannot be modified once it is created. An attempt to add, remove, or modify elements in the List after it is created will throw an exception.
The primary benefit of using immutable lists is that they provide thread safety and make the code more robust. Since the List cannot be modified after it is created, there is no risk of multiple threads trying to modify it simultaneously and causing issues. Additionally, immutable lists can be easily shared among different program parts without fear of being unintentionally modified.
Overall, using immutable lists in Java can improve the safety and robustness of a program, especially in multi-threaded environments where shared data structures can cause issues.
In Java, the ImmutableList class is a part of the Guava library, which provides several immutable collection classes. To use ImmutableList, we first import the com.google.common.collect the package containing the ImmutableList class.
The class declaration of ImmutableList is as follows:
ImmutableList extends the ImmutableCollection class and implements the List interface. It is a generic class, which means we can create an ImmutableList of any data type. The E in the declaration represents the type parameter, which we can replace with any class or interface name.
The ImmutableList class implements the List interface and represents a list that cannot be modified once created.
The class hierarchy of ImmutableList is as follows:
Here, ImmutableCollection is an abstract class that provides a skeletal implementation of the ImmutableCollection interface, which ImmutableList extends.
Overall, the ImmutableList class provides a convenient and efficient way to create and use immutable lists in Java.
There are different ways to create an ImmutableList in Java, depending on the version of Java you are using and the libraries you have available. Here are some examples:
1. Using the Java 9 of() method:
Java 9 introduced a new method called of() in the List interface, which creates immutable lists more concisely and readably. The of() method is a factory method that takes a variable number of arguments and returns an immutable list containing those elements. It can be used with any class that implements the List interface, including ArrayList, LinkedList, and ImmutableList. One advantage of using the of() method is that it is much more concise and provides compile-time safety by performing type inference on the arguments, ensuring that only objects of the correct type are added to the List. Overall, the of() method simplifies the creation of immutable lists in Java.
The steps to find the solution is mentioned below.
Fruits: [apple, banana, orange, grape] Cannot modify immutable List.
2. Using the ImmutableList.Builder class from the Guava library:
Adding elements to the List in a fluent style, making it convenient for incrementally creating a list.
Regardless of the method used, the resulting ImmutableList can be accessed and iterated over like any other list, but its contents cannot be modified.
Here is the stepwise solution for the given code:
Immutable List 1: [Welcome, to, home] Immutable List 2: [Welcome, to, home, Think] Immutable List 3: [Welcome, to, home, Think, Big]
3. By using of() method of ImmutableList class
The of() method of the ImmutableList class in the Guava library allows you to create an immutable list with a fixed number of elements. Once the List is created, you cannot add, remove or modify its elements.
Fruits: [apple, banana, orange, grape]
4. By use of copyOf() method
In Java, the copyOf() method creates a new array that copies an existing array with a specified length. The method takes two arguments: the array to be copied and the length of the new array.
Immutable List 1: [Java, Python, C++] Immutable List 2: [Learning, Web, Development, is, Fun]
The program illustrates creating an immutable list in Java using the Collections.unmodifiableList method. Additionally, it shows how to handle the UnsupportedOperationException that is thrown when trying to modify the List.
The steps to find the solution is mentioned below:
Note that the Collections.unmodifiableList method only creates an immutable view of the original List. The immutable view will reflect those changes if the original List is modified. To create a truly immutable list that cannot be modified by any means, you can use a custom implementation of the List interface that throws an exception when attempting to modify the List.
UnsupportedOperationException: null UnsupportedOperationException: null UnsupportedOperationException: null
Collections.unmodifiableList() is a method in the Java Collections Framework that creates an unmodifiable view of an existing list. It can be inferred that trying to modify the unmodifiable List will lead to the occurrence of an UnsupportedOperationException. The original List can still be modified, and any changes will be reflected in the unmodifiable List.
The program showcases how to utilize the Collections.unmodifiableList() method to generate an unmodifiable representation of a mutable list.
The steps to find the solution is mentioned below:
Attempt to modify unmodifiableList failed: null mutableList: [apple, banana, orange, pear] unmodifiableList: [apple, banana, orange, pear]
Advantages of ImmutableList
ImmutableList has several advantages, including: