Alex Rivera | Logout

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

Asked 2010-02-02T16:39:04.940
64

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)?
Edit
Report

1 Answer

7

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

answered 2010-02-02T16:50:01.277

Your Answer