Introduction
Java 10 introduced a new method called List.copyOf()
. This method creates an unmodifiable copy of an existing list, meaning you can’t change the list after you create it. This is useful when you want to share a list with other parts of your program, but ensure that the list cannot be modified.
Key Points:
- Immutable Copy:
List.copyOf()
creates a list that cannot be changed. - Shallow Copy: The elements themselves are not copied; only their references are.
- No Null Elements: The original list cannot contain
null
elements. - Read-Only: Attempting to modify the list will result in an error.
How to Use List.copyOf()
To create an unmodifiable list, you can use the List.copyOf()
method and pass an existing list to it.
Syntax:
List<Type> unmodifiableList = List.copyOf(originalList);
Here, originalList
is the list you want to copy, and unmodifiableList
is the new list that cannot be modified.
Example of Using List.copyOf()
Basic Example
Here’s a simple example demonstrating the use of List.copyOf()
with Indian names:
import java.util.*;
public class ListCopyOfExample {
public static void main(String[] args) {
List<String> originalList = Arrays.asList("Ananya", "Rohit", "Lakshmi");
// Create an unmodifiable copy of the original list
List<String> copyList = List.copyOf(originalList);
System.out.println("Original List: " + originalList);
System.out.println("Copy List: " + copyList);
// Attempt to modify the copy
try {
copyList.add("Vikram"); // This will throw UnsupportedOperationException
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify copyList: " + e.getMessage());
}
}
}
Output:
Original List: [Ananya, Rohit, Lakshmi]
Copy List: [Ananya, Rohit, Lakshmi]
Cannot modify copyList: null
Explanation:
- Immutable Copy: The
List.copyOf()
method creates a new list that cannot be changed, ensuring the original list’s content is protected. - Error on Modification: Any attempt to modify the resulting list will cause an
UnsupportedOperationException
to be thrown.
Null Safety
The List.copyOf()
method does not allow null elements in the original list. If the original list contains null
, a NullPointerException
is thrown.
Example:
import java.util.*;
public class NullSafetyExample {
public static void main(String[] args) {
List<String> originalList = Arrays.asList("Ananya", null, "Lakshmi");
try {
// Attempt to create an unmodifiable copy of a list with null elements
List<String> copyList = List.copyOf(originalList);
} catch (NullPointerException e) {
System.out.println("List contains null elements: " + e.getMessage());
}
}
}
Output:
List contains null elements: null
Explanation:
- No Null Elements: The
List.copyOf()
method ensures that the copied list does not contain null elements. If the original list contains null elements, aNullPointerException
is thrown.
Real-World Example
Using List.copyOf()
is beneficial when you want to share a list with other parts of your program while ensuring it remains unchanged.
Example with Fruits List:
import java.util.*;
public class FruitListExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Mango");
fruits.add("Banana");
fruits.add("Apple");
// Create an unmodifiable copy of the fruits list
List<String> unmodifiableFruits = List.copyOf(fruits);
System.out.println("Unmodifiable Fruits List: " + unmodifiableFruits);
// Modify the original list
fruits.add("Orange");
System.out.println("Original Fruits List After Modification: " + fruits);
// Unmodifiable list remains unchanged
System.out.println("Unmodifiable Fruits List After Modification: " + unmodifiableFruits);
// Attempt to modify the unmodifiable list
try {
unmodifiableFruits.add("Grapes"); // This will throw UnsupportedOperationException
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify unmodifiableFruits: " + e.getMessage());
}
}
}
Output:
Unmodifiable Fruits List: [Mango, Banana, Apple]
Original Fruits List After Modification: [Mango, Banana, Apple, Orange]
Unmodifiable Fruits List After Modification: [Mango, Banana, Apple]
Cannot modify unmodifiableFruits: null
Explanation:
- Immutable List: The
List.copyOf()
method creates a read-only copy of the fruits list, ensuring the copied list remains unchanged even after the original list is modified. - Attempt to Modify: Attempting to add an element to the unmodifiable list throws an
UnsupportedOperationException
.
Conclusion
The List.copyOf()
method in Java 10 makes it easy to create unmodifiable copies of lists. This feature is helpful for creating read-only views of lists, ensuring the original data remains safe from accidental modification.
Summary:
- Immutable Copy:
List.copyOf()
creates a list that cannot be changed. - Shallow Copy: Only references are copied, not the elements themselves.
- No Null Elements: The original list cannot contain
null
elements. - Read-Only: The resulting list is read-only, preventing any modifications.
By using List.copyOf()
, you can create safe, immutable lists in your Java applications, making your code more robust and reliable.