Once upon a time, I watched a video lecture from the Princeton Coursera MOOC: an introduction to the algorithms that can be found here . It explains the cost of resizing an ArrayList structure when adding or removing elements from it. It turns out that if we want to resize our data structure, we will move from O(n) to amortized O(n) for add and remove operations.
I have been using Java ArrayList for several years. I was always sure that they grow and contract automatically. Only recently, to my great surprise, did I turn out to be wrong in this post . Java ArrayList do not shrink (although, of course, they do grow) automatically.
Here are my questions:
In my opinion, providing a reduction in ArrayList does no harm since the performance is already amortized O(n) . Why didn't the Java creators include this feature in the project?
I know that other data structures, such as HashMap , also do not shrink automatically. Is there any other data structure in Java that is built on top of arrays that support auto-reduction?
What are the trends in other languages? How automatic reduction looks in the case of lists, dictionaries, maps, sets in Python / C #, etc. If they go in the opposite direction to what Java does, then my question is: why?
source share