Long time ago I watched a video lecture from the Princeton Coursera MOOC: Introduction to algorithms, which can be found here. It explains the cost of resizing an ArrayList like structure while adding or removing the elements from it. It turns out that if we want to supply resizing to our data structure we will go from O(n) to amortized O(n) for add and remove operations.
I have been using Java ArrayList for a couple of years. I've been always sure that they grow and shrink automatically. Only recently, to my great surprise, I was proven wrong in this post. Java ArrayLists do not shrink (even though, of course they do grow) automatically.
Here are my questions:
In my opinion providing shrinking in
ArrayLists does not make any harm as the performance is alreadyamortized O(n). Why did Java creators did not include this feature into the design?I know that other data structures like
HashMaps also do not shrink automatically. Is there any other data structure in Java which is build on top of arrays that supports automatic shrinking?What are the tendencies in other languages? How does automatic shrinking look like in 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?
ArrayListthat automatically shrinks, and I would be very surprised to find one doing that. If you as anArrayListuser know that your list capacity can now be reduced, you could usetrimToSize().Oterms, you lose "nothing" if you provide automatic shrinking, like presented in the video. The complexity foraddandremoveare stillamortized O(n)-1.ArrayListops preferable to occasional hiccups or custom assembly code inSystem.arrayCopybeing so blazingly fast that it leaves auto-resize in dust - I can't say I see benefit in a) paying guaranteedO(N)each time we invokeremove()and b) burdening the client with manually trimming the list if/when needed, the latter being especially obnoxious. It definitely feels like a mistake. Venturing a guess, I'd say they went with a), though honestly, if you're that concerned with perfomance, you should be using arrays directly anyway.