Create Read-Only List in Java

1. Introduction

Creating a read-only list in Java ensures that the list remains unmodifiable after its creation, which is essential for maintaining data integrity and avoiding unintentional modifications. Java provides several ways to create read-only lists, including using the Collections utility class and the List.of method introduced in Java 9. This blog post will explore how to create and use read-only lists in Java.

2. Program Steps

1. Create a read-only list using Collections.unmodifiableList.

2. Create a read-only list using List.of (Java 9 and later).

3. Attempt to modify the read-only lists to demonstrate immutability.

3. Code Program

import java.util.*;

public class ReadOnlyListExample {
    public static void main(String[] args) {
        // Step 1: Creating a read-only list using Collections.unmodifiableList
        List<String> originalList = new ArrayList<>(Arrays.asList("Apple", "Banana", "Cherry"));
        List<String> unmodifiableList = Collections.unmodifiableList(originalList);
        System.out.println("Unmodifiable List: " + unmodifiableList);

        // Step 2: Creating a read-only list using List.of (Java 9 and later)
        List<String> readOnlyList = List.of("Apple", "Banana", "Cherry");
        System.out.println("Read-Only List using List.of: " + readOnlyList);

        // Step 3: Attempting to modify the read-only lists
        try {
            unmodifiableList.add("Date");
        } catch (UnsupportedOperationException e) {
            System.out.println("Cannot modify unmodifiableList: UnsupportedOperationException");
        }

        try {
            readOnlyList.add("Date");
        } catch (UnsupportedOperationException e) {
            System.out.println("Cannot modify readOnlyList using List.of: UnsupportedOperationException");
        }
    }
}

Output:

Unmodifiable List: [Apple, Banana, Cherry]
Read-Only List using List.of: [Apple, Banana, Cherry]
Cannot modify unmodifiableList: UnsupportedOperationException
Cannot modify readOnlyList using List.of: UnsupportedOperationException

Explanation:

1. The program begins by creating an ArrayList named originalList and populating it with some elements. This list is mutable and can be modified.

2. An unmodifiable version of originalList is created using Collections.unmodifiableList(originalList). This method returns a view of the original list that cannot be modified. Attempting to modify this list, such as by adding or removing elements, will result in an UnsupportedOperationException.

3. The program also demonstrates creating a read-only list using List.of("Apple", "Banana", "Cherry"), a feature introduced in Java 9. Lists created with List.of are inherently unmodifiable and cannot be altered after creation.

4. Attempts to modify both the unmodifiable list created with Collections.unmodifiableList and the read-only list created with List.of result in UnsupportedOperationException, illustrating the immutability of these lists.

5. This example highlights two approaches to creating read-only lists in Java, showing how to enforce immutability to ensure data integrity and prevent accidental modifications.

Comments