Hot questions for Operating ImmutableList in Guava
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:
- if I have
Dimensionobjects (for example) then I can't change any
Dimensionobject in it?
- and if I have
Dimensionobjects then I can't only add or delete any object but I can change them (for example call
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.
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);
List<Blah> foo; ... return new ImmutableList.Builder<Blah>().addAll(foo).build();
Answer: I don't see any reason why you should use builder here:
ImmutableList.copyOfis much more readable than making a Builder in this case,
Builderdoesn't infer generic type and you have to specify type by yourself when used as one-liner,
ImmutableList.copyOfdoes good magic when invoked with another immutable collection (attempts to avoid actually copying the data when it is safe to do so),
- Builder#addAll invokes
addAllon previously created
copyOfavoids creating any list for zero- and one-element collections (returns empty immutable list and singleton immutable list respectively),
copyOf(Collection)instance doesn't create temporary
- moreover, Builder#build invokes
copyOfon previously internally populated
ArrayList, what brings you to your question - why use
Builderhere, when you have
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.