Why do some collection data structures not maintain the order of insertion? What is the special thing achieved compared to maintaining order of insertion? Do we gain someth
I can't cite a reference, but by design the List
and Set
implementations of the Collection
interface are basically extendable Array
s. As Collections
by default offer methods to dynamically add and remove elements at any point -- which Array
s don't -- insertion order might not be preserved.
Thus, as there are more methods for content manipulation, there is a need for special implementations that do preserve order.
Another point is performance, as the most well performing Collection
might not be that, which preserves its insertion order. I'm however not sure, how exactly Collections
manage their content for performance increases.
So, in short, the two major reasons I can think of why there are order-preserving Collection
implementations are: