Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Immutable List in java?

People also ask

How do you make an ArrayList immutable in Java?

No, you cannot make the elements of an array immutable. But the unmodifiableList() method of the java. util. Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object.

Can ArrayList make immutable?

This quick tutorial will show how to make an ArrayList immutable with the core JDK, with Guava and finally with Apache Commons Collections 4. This article is part of the “Java – Back to Basic” series here on Baeldung.

How do you make a list mutable in Java?

One simple way: Foo[] array = ...; List<Foo> list = new ArrayList<Foo>(Arrays. asList(array)); That will create a mutable list - but it will be a copy of the original array.


Once your beanList has been initialized, you can do

beanList = Collections.unmodifiableList(beanList);

to make it unmodifiable. (See Immutable vs Unmodifiable collection)

If you have both internal methods that should be able to modify the list, and public methods that should not allow modification, I'd suggest you do

// public facing method where clients should not be able to modify list    
public List<Bean> getImmutableList(int size) {
    return Collections.unmodifiableList(getMutableList(size));
}

// private internal method (to be used from main in your case)
private List<Bean> getMutableList(int size) {
    List<Bean> beanList = new ArrayList<Bean>();
    int i = 0;

    while(i < size) {
        Bean bean = new Bean("name" + i, "address" + i, i + 18);
        beanList.add(bean);
        i++;
    }
    return beanList;
}

(Your Bean objects already seem immutable.)


As a side-note: If you happen to be using Java 8+, your getMutableList can be expressed as follows:

return IntStream.range(0,  size)
                .mapToObj(i -> new Bean("name" + i, "address" + i, i + 18))
                .collect(Collectors.toCollection(ArrayList::new));

In JDK 8:

List<String> stringList = Arrays.asList("a", "b", "c");
stringList = Collections.unmodifiableList(stringList);

In JDK 9:

List stringList = List.of("a", "b", "c");

reference


Use Collections.unmodifiableList(). You pass in your original ArrayList and it returns a list that throws an exception if you try to add, remove or shift elements. For example, use return Collections.unmodifiableList(beanList); instead of return beanList; at the end of getImmutableList(). main() will throw an exception. The Collections class has methods for all of the other common collection types besides List as well.


From Java 10 on, List.copyOf(Collection) can be used to return an unmodifiable list from the given collection. From source code of List.copyOf method:

  • if the given collection is an unmodifiable List, List.copyOf() will not create a copy.

  • if the given collection is mutable and modified, the returned list will not reflect such modifications. Meaning they are independent.