62

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)?
gpampara
  • 11,989
  • 3
  • 27
  • 26
Roman
  • 64,384
  • 92
  • 238
  • 332
  • 1
    You might also want to take a look at [What's the difference between Collections.unmodifiableSet() and ImmutableSet of Guava](http://stackoverflow.com/q/5611324/977087) – Crowie Apr 22 '15 at 15:35

5 Answers5

63

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.

Yishai
  • 90,445
  • 31
  • 189
  • 263
  • @Vuntic, if the class is truly immutable, its fields are declared final, and reflection can't change that. – Yishai Mar 03 '11 at 14:37
  • 4
    @Yishai, reflection *can* modify final fields. Try it. – amara Mar 06 '11 at 06:56
  • @Vuntic, with setAccessible? That depends on the security manager ;). – Yishai Mar 06 '11 at 16:27
  • @Yishai... yes, it does depend on the security manager. The normal security manager allows this. The question makes no reference to security managers. And that (setAccessible) is not even how you modify final fields. – amara Mar 06 '11 at 17:10
  • 5
    @Vuntic, I get an exception when I try to modify a final field without setting setAccessable. What are you thinking of? BTW, the question made no reference to reflection either ;) – Yishai Mar 06 '11 at 18:19
  • 2
    @Yishai I know I'm 7 years late to this conversation... but for the public record you modify final fields by first using reflection to remove the final modifier. It only works on Objects though, not on primitives. – James Dunn Feb 08 '18 at 23:27
20

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.

Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129
  • 3
    "As the documentation says, Google's code creates a copy." It *might* create a copy. If the original is also an immutable data structure, it might actually be able to reuse it. So if you use `ImmutableList` extensively, you'll hit this case very often and will end up not having to copy very much. – Luis Casillas Mar 18 '14 at 04:58
7

ImmutableList is similar to Collections.unmodifiableList( new ArrayList( list ) ) . Note that the newly created ArrayList is not assigned to a field or variable.

KitsuneYMG
  • 12,753
  • 4
  • 37
  • 58
3
  1. No, the contained individual objects can still be modified. A collection is really only storing references to the contained objects, not a full copy of every object.
  2. You can modify the list by modifying the parent Collection you called Collections.unmodifiableList(list) on. But yes, you CAN use setDimension to change a stored list element.
BobMcGee
  • 19,824
  • 10
  • 45
  • 57
0

You might also want to take a look at this question (What's the difference between Collections.unmodifiableSet() and ImmutableSet of Guava).

Community
  • 1
  • 1
Bartek
  • 1,327
  • 1
  • 11
  • 22