4 ms·
How does this maintain the List interface, which defines methods for mutating the collection through methods such as add() and remove()? If I get a variable of
by mhansen 17y ago
How does this maintain the List interface, which defines methods for mutating the collection through methods such as add() and remove()?
If I get a variable of type List, I expect to be able to change the list.
- _delirium 17y agoThe List interface labels all the methods that could change a list as "optional operation", which implementing classes are free to throw an UnsupportedOperationException for: http://java.sun.com/javase/6/docs/api/java/util/List.html http://java.sun.com/javase/6/docs/api/java/util/List.html The current API already uses this in a bunch of places to return immutable Lists. For example, the lists you get out of Collections.singletonList() or Collections.nCopies() are immutable. That's a bit broken from an OO perspective (is the interface an interface or not?), but it's a better solution than the const v. non-const container mess that C++/STL has, at least.