
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: April 4, 2025
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.
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<>(Arrays.asList("one", "two", "three"));
List<String> unmodifiableList = Collections.unmodifiableList(list);
unmodifiableList.add("four");
}
Since Java 9, we can use a List<E>.of(E… elements) static factory method to create an immutable list:
@Test(expected = UnsupportedOperationException.class)
public final void givenUsingTheJava9_whenUnmodifiableListIsCreated_thenNotModifiable() {
final List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
final List<String> unmodifiableList = List.of(list.toArray(new String[]{}));
unmodifiableList.add("four");
}
Notice how we have to convert the existing list into an array. This is because List.of(elements) accepts vararg parameters.
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<>(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 return the strong-typed ImmutableList instead of simply List:
@Test(expected = UnsupportedOperationException.class)
public void givenUsingGuavaBuilder_whenUnmodifiableListIsCreated_thenNoLongerModifiable() {
List<String> list = new ArrayList<>(Arrays.asList("one", "two", "three"));
ImmutableList<String> unmodifiableList = ImmutableList.<String>builder().addAll(list).build();
unmodifiableList.add("four");
}
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<>(Arrays.asList("one", "two", "three"));
List<String> unmodifiableList = ListUtils.unmodifiableList(list);
unmodifiableList.add("four");
}
This tutorial illustrates how to easily create an unmodifiable List out of an existing ArrayList using either the core JDK, Google Guava or Apache Commons Collections.