I'm curious what is considered a better practice when returning a collection of objects from a mutable class:
public class Someclass {
public List<String> strings;
public add(String in){ strings.add(in); }
public remove(String in) { strings.remove(in); }
//THIS
public List<String> getStrings(){
return Collections.unmodifiableList(strings);
}
//OR THIS
public List<String> getStrings(){
return new ArrayList(strings);
}
}
I always assumed wrapping the internal collection in an Unmodifiable was the best approach since I didn't see a reason to incur the overhead of creating a copy. However, it occurred to me that any changes made to the list internally would be exposed to the client which strikes me as bad.