javaguavaimmutabilitydefensive-copy

Passing an immutable list to ImmutableList.copyOf()?


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.


Solution

  • Couple things (mostly covered in the comments, but as an answer):