Does anyone have any idea, why removeRange method in AbstractList (and also in ArrayList) is protected
? It looks like a quite well-defined and useful operation, but still, to use it, we're forced to subclass the List implementation.
Is there some hidden rationale? Seems quite inexplicable to me.
Yes, because that's not how you remove a range from outside code. Instead, do this:
This actually calls
removeRange
behind the scenes.†The OP asks why
removeRange
is not part of theList
public API. The reason is described in Item 40 of Effective Java 2nd ed, and I quote it here:One can argue that
removeRange
doesn't have that many parameters and is therefore probably not a candidate for this treatment, but given that there's a way to invokeremoveRange
through thesubList
, there is no reason to clutter up theList
interface with a redundant method.† The
AbstractList.removeRange
documentation says:Also, see OpenJDK's implementation of
AbstractList.clear
andSubList.removeRange
.