What is the purpose of the Guava ImmutableList class?
A) To create immutable lists that cannot be modified after creation
B) To perform I/O operations
C) To handle database connections
D) To manage user interfaces
Answer:
A) To create immutable lists that cannot be modified after creation
Explanation:
The Guava ImmutableList
class is used to create immutable lists in Java, meaning that the list cannot be modified after it is created. Immutable collections are useful in scenarios where you need to ensure that the data remains constant and is not accidentally changed by other parts of the code.
For example:
ImmutableList<String> immutableList = ImmutableList.of("one", "two", "three");
This creates an immutable list containing the elements “one”, “two”, and “three”. Any attempt to modify this list (e.g., adding or removing elements) will result in an UnsupportedOperationException
.