Java collections maintaining insertion order

前端 未结 10 1132
忘了有多久
忘了有多久 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条回答
  •  感动是毒
    2020-12-13 00:16

    • The insertion order is inherently not maintained in hash tables - that's just how they work (read the linked-to article to understand the details). It's possible to add logic to maintain the insertion order (as in the LinkedHashMap), but that takes more code, and at runtime more memory and more time. The performance loss is usually not significant, but it can be.
    • For TreeSet/Map, the main reason to use them is the natural iteration order and other functionality added in the SortedSet/Map interface.

提交回复
热议问题