I have the following code:
private static final ImmutableMultimap<String, String> namesToAddress;
public static List<String> getAddresses(String name){
return ImmutableList.copyOf(namesToAddress.get(name));
}
My question is wheter the defensive copyOf() here is necessary, as the get() returns an immutable list anyway?
Note I am using ImmutableMultiimap from Google Guava.
Thanks.
Couple things (mostly covered in the comments, but as an answer):
ImmutableListMultimapas the type fornamesToAddresses,get()will return anImmutableList; no need to callcopyOfor cast or anythingImmutableMultimap.get()will return anImmutableCollection;ImmutableCollections have anasList()method to view (or copy if necessary) the collection as anImmutableListImmutableList.copyOf(ImmutableCollection)will end up callingasList()on the collection anyway