Implement an immutable collection holder

 To implement an immutable collection holder, you need to ensure that the list of strings it holds cannot be modified after it is assigned. This can be done by creating an unmodifiable copy of the list in the constructor and returning an unmodifiable view of the list in the getter method. Using Collections.unmodifiableList is a common way to achieve this in Java.

import java.util.List;

import java.util.ArrayList;

import java.util.Collections;


public class Main {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("apple", "banana", "cherry");

        ImmutableCollectionHolder holder = Answer.createImmutableCollectionHolder(list);


        System.out.println("Immutable list: " + holder.getItems());


        // Try to modify the list through the getter

        try {

            holder.getItems().add("date");

        } catch (UnsupportedOperationException e) {

            System.out.println("Modification attempt failed: " + e);

        }


        // Try to modify the original list

        list.set(1, "blueberry");

        System.out.println("Immutable list after modifying original list: " + holder.getItems());

    }

}

class Answer {


    // Change these boolean values to control whether you see 

    // the expected result and/or hints.

    static boolean showExpectedResult = true;

    static boolean showHints = true;


    // Create a new ImmutableCollectionHolder with a list

    static ImmutableCollectionHolder createImmutableCollectionHolder(List<String> strings) {

        return new ImmutableCollectionHolder(strings);

    }


}


class ImmutableCollectionHolder {

    private final List<String> items;


    public ImmutableCollectionHolder(List<String> items) {

        // Create an unmodifiable copy of the list

        this.items = Collections.unmodifiableList(new ArrayList<>(items));

    }


    public List<String> getItems() {

        // Return the unmodifiable list

        return items;

    }

}

Explanation:

  1. Constructor:

    • The constructor takes a List<String> as an argument.
    • It creates a new ArrayList from the provided list to ensure that any subsequent modifications to the original list do not affect the internal list of the ImmutableCollectionHolder.
    • The Collections.unmodifiableList method is used to create an unmodifiable view of this new list, ensuring that the internal list cannot be modified.
  2. Getter:

    • The getItems method returns the unmodifiable list, ensuring that callers cannot modify the internal list.

Output:

Immutable list: [apple, banana, cherry]

Modification attempt failed: java.lang.UnsupportedOperationException

Immutable list after modifying original list: [apple, banana, cherry]


The ImmutableCollectionHolder ensures immutability by creating an unmodifiable copy of the input list and returning an unmodifiable view of this list. Any attempts to modify the list through the getter method will result in an UnsupportedOperationException, and changes to the original list will not affect the internal list of the ImmutableCollectionHolder.


Comments

Popular posts from this blog

Transform values with a stream

Collections Framework

Inspect a collection