Does Java's LinkedHashMap maintain the order of keys? [duplicate]

人走茶凉 提交于 2019-11-27 02:02:32

问题


When LinkedHashMap.keySet() is called, will the order of the Set returned be the same as the order the keys were added in?


回答1:


Yes.

See: LinkedHashMap:

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

and from the HashMap#keySet documentation:

The set [returned] is backed by the map, so changes to the map are reflected in the set, and vice-versa.




回答2:


Yes. The exception is that when a key is reinserted, it appears in the order in which it was first inserted to the list.



来源:https://stackoverflow.com/questions/3478061/does-javas-linkedhashmap-maintain-the-order-of-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!