I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

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.

2. With the JDK

First, the JDK provides a nice way to get an unmodifiable collection out of an existing one:

Collections.unmodifiableList(list);

The new collection should no longer be modifiable at this point:

@Test(expected = UnsupportedOperationException.class)
public void givenUsingTheJdk_whenUnmodifiableListIsCreated_thenNotModifiable() {
    List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
    List<String> unmodifiableList = Collections.unmodifiableList(list);
    unmodifiableList.add("four");
}

3. With Guava

Guava provides similar functionality for creating its own version of ImmutableList:

ImmutableList.copyOf(list);

Similarly – the resulting list should not be modifiable:

@Test(expected = UnsupportedOperationException.class)
public void givenUsingGuava_whenUnmodifiableListIsCreated_thenNotModifiable() {
    List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
    List<String> unmodifiableList = ImmutableList.copyOf(list);
    unmodifiableList.add("four");
}

Note that this operation will actually create a copy of the original list, not just a view.

Guava also provides a builder – this will returned the strong-typed ImmutableList instead of simply List:

@Test(expected = UnsupportedOperationException.class)
public void givenUsingGuavaBuilder_whenUnmodifiableListIsCreated_thenNoLongerModifiable() {
    List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
    ImmutableList<Object> unmodifiableList = ImmutableList.builder().addAll(list).build();
    unmodifiableList.add("four");
}

4. With the Apache Collections Commons

Finally, Commons Collection also provides an API to create an unmodifiable list:

ListUtils.unmodifiableList(list);

And again, modifying the resulting list should result in an UnsupportedOperationException:

@Test(expected = UnsupportedOperationException.class)
public void givenUsingCommonsCollections_whenUnmodifiableListIsCreated_thenNotModifiable() {
    List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
    List<String> unmodifiableList = ListUtils.unmodifiableList(list);
    unmodifiableList.add("four");
}

5. Conclusion

This tutorial illustrates how to easily create a unmodifiable List out of an existing ArrayList using either the core JDK, Google Guava or Apache Commons Collections.

The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS

Sort by:   newest | oldest | most voted
Bret Godwin
Guest

Arrays.asList() gets you most of the way there, but set() still works, so yes, unmodifiableList is the way to go. Nice article.

gyoder
Guest

Of course, the problem with method 2 is that the original list is still modifiable…

@Test
public void thatUnmodifiableDoesntWork() {

List mylist = Arrays.asList(1, 2, 3, 4, 5);
List unmodifiable = Collections.unmodifiableList(mylist);
Integer newVal = new Integer(99);
mylist.set(3, newVal);
assertEquals(newVal, unmodifiable.get(3));

}

Eugen Paraschiv
Guest

Yes – if the original list was modifiable, there’s really nothing you can do about it to make it unmodifiable. So, the client code needs to be aware of that and discard the reference to the original so that there’s no chance of it being modified. Cheers,
Eugen.

Kalle Eidhagen
Guest

Actually,

@Test
public void thatUnmodifiableCanBeMadeToWork() {

List mylist = Arrays.asList(1, 2, 3, 4, 5);
List unmodifiable = Collections.unmodifiableList(new ArrayList(mylist));
Integer newVal = new Integer(99);
mylist.set(3, newVal);
assertNotEquals(newVal, unmodifiable.get(3));

}
would make it work.

Eugen Paraschiv
Guest

You’re right – defensive copying makes sense if you need to decouple the two lists – I agree. The original list doesn’t become immutable, but it doesn’t matter. Thanks,
Eugen.

Manabendra Sarkar
Guest

if we already know the fixed list, and want it to be immutable, why to create the original list as a separate variable? We can create the unmodifiable object directly.

List unmodifiable = Collections.unmodifiableList(new ArrayList() {
{
add(1);
add(2);
add(3);

}
};

Eugen Paraschiv
Guest

We can, yes. The examples are leaning towards the fact that you don’t actually know the original list. Of course the examples simplify that and also create the original, but that’s not usually the case.
Cheers,
Eugen.

Imteyaz Ahmad
Guest

It would be great if u can explain pros and cons of using different libraries for achieving the same thing…btw…thanks for the sharing this.

Eugen Paraschiv
Guest

Hey Ahmad – usually I do, but for these kinds of low level tasks, there’s very little that goes into it other than personal preference and the question of – is the library available in the project already or not. Cheers,
Eugen.

Manish Dubey
Guest

Nice Article for list immutable. Would be great if you provide pron and cons of theses.

Eugen Paraschiv
Guest

Pros and cons of immutability? That’s a good idea, but it needs to be its own article (there’s to much to cover here). Thanks,
Eugen.