posted 10 years ago
Well, to be fair, the Collection interface lists the method as optional. It can throw an UnsupportedOperationException in some cases. So in truth, there is no general answer to Imran's last question; it depends on the specific Collection implementation that we are talking about. Most collections implement this method, but a few, like the implementations returned by Collections.unmodifiableXXX() and Arrays.asList(), do not. The idea of optional methods in an interface is a bit odd, and in Java I think it's unique to the collections framework. Generally an interface promises that a method will be available in an implementing class. But with an optional method, the promise is that the method will be present in the class, but it may throw an exception. So the method is available, except that it really isn't.