Java collections maintaining insertion order

前端 未结 10 1108
忘了有多久
忘了有多久 2020-12-13 00:06

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

10条回答
  •  -上瘾入骨i
    2020-12-13 00:38

    I can't cite a reference, but by design the List and Set implementations of the Collection interface are basically extendable Arrays. As Collections by default offer methods to dynamically add and remove elements at any point -- which Arrays 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:

    1. Class architecture
    2. Performance

提交回复
热议问题