Seeing as how there are no distinct Unmodifiable Collections interfaces, aren't you just setting yourself up for runtime exceptions by returning Unmodifiable Collections from method invocations? example:
public class Start {
public static void main(String[] args) {
try {
List<String> listA = getListA();
listA.add("whatever");
System.out.println("listA: " + listA.get(0));
List<String> listB = getListB();
listB.add("whatever");
} catch(Exception e) {
System.out.println("SURPRISE!!! you should have known that listB was unmodifible.");
}
}
static List<String> getListA() {
return new ArrayList();
}
static List<String> getListB() {
return Collections.unmodifiableList(new ArrayList());
}
}
- At best, in javadoc I could write in bold letters "the returned Collection is Unmodifiable!"?
- In what scenario is returning Unmodifiable Collections worth the risk of runtime exceptions?