Hot questions for Operating ImmutableList in Guava

Top 10 Java Open Source / Guava / Operating ImmutableList

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

Question: 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)?

Answer: 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.

UnmodifiableMap (Java Collections) vs ImmutableMap (Google)

Question: I've seen lots of references to UnmodifiableMap and ImmutableMap online, but I don't see anything comparing/contrasting them. I figure there is a good reason that Google/Guava created their own version - can someone tell me what it is?

Answer: An unmodifiable map may still change. It is only a view on a modifiable map, and changes in the backing map will be visible through the unmodifiable map. The unmodifiable map only prevents modifications for those who only have the reference to the unmodifiable view:

Map<String, String> realMap = new HashMap<String, String>();
realMap.put("A", "B");

Map<String, String> unmodifiableMap = Collections.unmodifiableMap(realMap);

// This is not possible: It would throw an 
// UnsupportedOperationException
//unmodifiableMap.put("C", "D");

// This is still possible:
realMap.put("E", "F");

// The change in the "realMap" is now also visible
// in the "unmodifiableMap". So the unmodifiableMap
// has changed after it has been created.
unmodifiableMap.get("E"); // Will return "F". 

In contrast to that, the ImmutableMap of Guava is really immutable: It is a true copy of a given map, and nobody may modify this ImmutableMap in any way.

an immutable map can also be created with the standard API using

Map<String, String> immutableMap = 
    Collections.unmodifiableMap(new LinkedHashMap<String, String>(realMap)); 

This will create an unmodifiable view on a true copy of the given map, and thus nicely emulates the characteristics of the ImmutableMap without having to add the dependency to Guava.

Guava ImmutableList copyOf vs Builder

Question: I was wondering which is more efficient and why?


List<Blah> foo;
return ImmutableList.copyOf(foo);

or 2)

List<Blah> foo;
return new ImmutableList.Builder<Blah>().addAll(foo).build();

Answer: I don't see any reason why you should use builder here:

  1. ImmutableList.copyOf is much more readable than making a Builder in this case,
  2. Builder doesn't infer generic type and you have to specify type by yourself when used as one-liner,
  3. ImmutableList.copyOf does good magic when invoked with another immutable collection (attempts to avoid actually copying the data when it is safe to do so),
  4. Builder#addAll invokes addAll on previously created ArrayList while copyOf avoids creating any list for zero- and one-element collections (returns empty immutable list and singleton immutable list respectively),
  5. copyOf(Collection) instance doesn't create temporary ArrayList (copyOf(Iterable) and copyOf(Iterator) does so),
  6. moreover, Builder#build invokes copyOf on previously internally populated ArrayList, what brings you to your question - why use Builder here, when you have copyOf?

Personally I use ImmutableList.builder() static factory instead of new ImmutableList.Builder<Blah>() constructor - when assigned to a Builder<Blah> variable the first infers generic type while the latter doesn't.