3

can anyone throw some light on why is removeRange(int fromIndex, int toIndex) in Java Vector is protected?

Syntax - protected synchronized void removeRange(int fromIndex, int toIndex)

I have studies a few blogs and did some code to understand but things are not really clear here. I have looked inside Vector.java and tried to create some understanding as well. But my overall perception is that removeRange(int fromIndex, int toIndex) will eventually get removed.

I felt sublist(int fromIndex, int toIndex).clear() is capable of doing the same job. And looks more apt from implementation and usability point of view.

Please help in understanding if you have a better thought.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Xat
  • 127
  • 1
  • 3
  • 10
  • 5
    "But my overall perception is that `removeRange(int fromIndex, int toIndex)` will eventually get removed." what makes you think it will? And why would you expect it to be `public` in the first place? – Federico klez Culloca Dec 01 '20 at 12:39
  • 4
    Maybe avoid using `java.util.Vector` at all. It is not usually the best option. – khelwood Dec 01 '20 at 12:39
  • Don't use `Vector`, ever! Use `ArrayList` instead – fps Dec 01 '20 at 13:22
  • @FedericoklezCulloca That's just loud thinking because I was not able to think about implementations where I need to use ```removeRange(int fromIndex, int toIndex)``` by forcing myself to extend Vector Class :) And I can always use```sublist(int fromIndex, int toIndex).clear()``` instead. Also, I want to hear a better answer from you :) So what happens when ```removeRange(int fromIndex, int toIndex)``` in Vector becomes Public? – Xat Dec 01 '20 at 15:46
  • 1
    Of course, “`sublist(int fromIndex, int toIndex).clear()` is capable of doing the same job”. It’s capable of doing it, because someone has implemented it. In case of `Vector` or any other subclass of `AbstractList`, e.g. `ArrayList`, it has been implemented via that protected method. Which is simpler than requiring every sub class to implement sub lists again. – Holger Dec 01 '20 at 15:51
  • @khelwood -Yes yes, I avoid using it. but this reasoning of it being protected is something now very clear to me :) – Xat Dec 01 '20 at 15:53
  • @Holger, what does ```protected ``` access achieve here that ```public``` cannot? – Xat Dec 01 '20 at 16:23
  • @fps ```ArrayList``` has got ```protected void removeRange(int fromIndex, int toIndex)``` too. Check the answer below. I understand the reason why Collection f/w is so protected about ```removeRange()``` ;) – Xat Dec 01 '20 at 16:42
  • 2
    @Akshat Declaring it `protected` achieves that you will use the standard API, i.e. `sublist(fromIndex, toIndex).clear()`, instead of using this implementation detail. – Holger Dec 01 '20 at 17:04

1 Answers1

6

It is there because it is inherited form AbstractList.removeRange. The intention for exposing it in this abstract base class is documented as enabling subclasses to improve the performance of clear():

This method is called by the clear operation on this list and its subLists. Overriding this method to take advantage of the internals of the list implementation can substantially improve the performance of the clear operation on this list and its subLists.

It is extremely unlikely that this method will ever be removed, as this would be a major compatibility problem.

But you are right that as a user of this class you should stick with the public interface, the protected methods are mostly useful for implementing subclasses.


So, why is the method not made public?

The JavaDocs for List.subList tell us

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

 list.subList(from, to).clear();

So not including range operations in the public interface was a deliberate design choice. On any List implementation, these operations can be performed by invoking operations on sublist views.

Hulk
  • 6,399
  • 1
  • 30
  • 52
  • I saw ```AbstractList.removeRange``` is kept protected and so when Vector class overrides it, it can be made public as well. But instead, it is kept as protected. The point of improving the performance of ```clear()``` you mentioned, I read it as well in ```AbstractList```. And I observed that Vector has its own ```clear()``` that calls ```removeAllElements()```. – Xat Dec 01 '20 at 16:18
  • @Holger, sorry I re-did my comment. – Xat Dec 01 '20 at 16:20
  • @Hulk I like that reasoning. And ```removeRange(int fromIndex, int toIndex)``` is not part of LinkedList maybe for a reason. I never gave thought to it. – Xat Dec 01 '20 at 16:29
  • @Hulk - So it's "by design", I have heard that one before ;) – Xat Dec 01 '20 at 16:36
  • @Hulk and Holger, thanks for your time and guidance. Really appreciate it :) – Xat Dec 01 '20 at 16:52