Java 的 LinkedHashMap 是否维护键的顺序?
调用LinkedHashMap.keySet()时,返回Set的顺序会和key的添加顺序一致吗?
解决方案是的.
参见:LinkedHashMap:p><块引用>
这个链表定义了迭代排序,通常是顺序其中钥匙被插入地图(插入顺序).
并来自 HashMap#keySet 文档:
<块引用>集合 [returned] 由地图支持,所以地图的变化反映在集合,反之亦然.
When LinkedHashMap.keySet() is called, will the order of the Set returned be the same as the order the keys were added in?
解决方案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.
相关文章