When using Guava ImmutableCollection as a parameter for a function, it is better to use the parameter type ImmutableCollection :
void <T> foo(ImmutableCollection<T> l)
or whether the function should take Collection<T> and create an immutable collection, as in
void <T> foo(Collection<T> l) { ImmutableCollection<T> l2 = ImmutableCollection.copyOf(l); // ... }
The first version seems preferable because the caller is sure that the card that he passes to the function is not changed by it. But the first version requires that the client code with the collection call copyOf() , copyOf() .:
Collection collection = map.values(); foo(ImmutableCollection.copyOf(collection));
PS: This is not entirely true, since ImmutableCollection does not have copyOf() , but ImmutableList and ImmutableSet do.
source share