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
removeRangebehind the scenes.†The OP asks why
removeRangeis not part of theListpublic API. The reason is described in Item 40 of Effective Java 2nd ed, and I quote it here:One can argue that
removeRangedoesn’t have that many parameters and is therefore probably not a candidate for this treatment, but given that there’s a way to invokeremoveRangethrough thesubList, there is no reason to clutter up theListinterface with a redundant method.† The
AbstractList.removeRangedocumentation says:Also, see OpenJDK’s implementation of
AbstractList.clearandSubList.removeRange.