Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between google's ImmutableList and Collections.unmodifiableList ()?

From ImmutableList javadocs:

Unlike Collections.unmodifiableList(java.util.List), which is a view of a separate collection that can still change, an instance of ImmutableList contains its own private data and will never change. ImmutableList is convenient for public static final lists ("constant lists") and also lets you easily make a "defensive copy" of a list provided to your class by a caller.

Does it mean that:

  1. if I have ImmutableList of Dimension objects (for example) then I can't change any Dimension object in it?
  2. and if I have Collections.unmodifiableList (list) of Dimension objects then I can't only add or delete any object but I can change them (for example call setDimension(width, height) method)?
like image 937
Roman Avatar asked Feb 02 '10 16:02

Roman


People also ask

What is an ImmutableList?

ImmutableList, as suggested by the name, is a type of List which is immutable. It means that the content of the List are fixed or constant after declaration, that is, they are read-only. If any attempt made to add, delete and update elements in the List, UnsupportedOperationException is thrown.

What is Unmodifiable collection?

Collections that do not support modification operations (such as add , remove and clear ) are referred to as unmodifiable. Collections that are not unmodifiable are modifiable. Collections that additionally guarantee that no change in the Collection object will be visible are referred to as immutable.

Is Unmodifiable list immutable?

If you create a List and pass it to the Collections. unmodifiableList method, then you get an unmodifiable view. The underlying list is still modifiable, and modifications to it are visible through the List that is returned, so it is not actually immutable.


2 Answers

Using Collections.unmodifiableList creates a wrapper around your List. if the underlying list changes, so does your unmodifiableList's view.

As the documentation says, Google's code creates a copy. It's a more expensive computation and consumes more memory, but if someone alters the original list, it cant affect the ImmutableList.

Neither of these will prevent you from changing an object in a list, or it's fields, or fields of fields, etc.

like image 44
Mark Bolusmjak Avatar answered Oct 18 '22 18:10

Mark Bolusmjak


No, the immutability is only applied to the amount and references of the objects in the Collection, and does not address the mutability of objects you put in the Collection.

What Immutable list gains over the standard JDK Collections.unmodifiableList is that by using ImmutableList you are guaranteed that the objects referenced, their order and the size of the list cannot change from any source. With Collections.unmodifiableList if something else has a reference to the underlying list, that code can modify the list even though you have a reference to an unmodifiable list.

If, however, you want true immutability, you have to fill the list with immutable objects.

like image 85
Yishai Avatar answered Oct 18 '22 19:10

Yishai