Take the 2-minute tour ×

It would be nice to turn objects of Java's List interface into an immutable equivalent at the point in time that mutation is no longer required. That is, the client can call a freeze method that makes the List immutable. The immediate benefit to me is thread-safety without the memory expense of deep copying. (Edit: People would be correct if they assume that one extra immutable copy, to be used by all threads, is affordable.)

Is there a third-party interface or class that provides such a feature?

share|improve this question
Do you mean one of these? download.oracle.com/javase/6/docs/api/java/util/…. You can build your own list (as a strategy), and when the freeze method is invoked, you change the internal list for an immutable list. –  Augusto Nov 18 '12 at 12:12
add comment

4 Answers

up vote 6 down vote accepted

How about Collections.unmodifiableList(List list)?

share|improve this answer
add comment

Try to use CopyOnWriteArrayList.

The CopyOnWriteArrayList behaves much like the ArrayList class, except that when the list is modified, instead of modifying the underlying array, a new array is created and the old array is discarded. This means that when a caller gets an iterator (i.e. copyOnWriteArrayListRef.iterator() ), which internally holds a reference to the underlying CopyOnWriteArrayList object’s array, which is immutable and therefore can be used for traversal without requiring either synchronization on the list copyOnWriteArrayListRef or need to clone() the copyOnWriteArrayListRef list before traversal (i.e. there is no risk of concurrent modification) and also offers better performance.

share|improve this answer
add comment

There is an ImmutableList class as part of the Guava libraries. You can use the copyOf method to create an ImmutableList from an existing Iterable, eg.

List<String> immutableList = ImmutableList.copyOf(list);

share|improve this answer
add comment

Direct using of Collections.unmodifiableList is not enough if the client still has a reference to the original mutable list.

I would create a delegating list implementation that would have an internal reference to the original mutable list (the delegate) and forward all of the method calls to it. It's a PITA to write such code by hand, but Eclipse for example can generate it automatically for you.

Then upon calling the freeze method, I would wrap the original list with the Collections.unmodifiableList which ensures that all of the future method calls to the FreezingList go to the original delegate only through the unmodifable view.

To make things more secure, but less flexible, you can change the following constructor and instead of passing the original list to it (which can still leave a reference to the original mutable list to the client) instantiate the list internally (for example as an ArrayList).

public class FreezingList<E> implements List<E> {

    // the original list you delegate to (the delegate)
    private List<E> list;

    private boolean frozen = false;

    public FreezingList(List<E> list) {
        this.list = list;

    public void freeze() {
        if (!frozen) {
            list = Collections.unmodifiableList(list);
            frozen = true;

    // all the delegating methods follow:
    public int size() {
        return list.size();

    public E get(int index) {
        return list.get(index);
    // etc. etc.
share|improve this answer
add comment

Your Answer


