Java 的 LinkedHashMap 是否维护键的顺序?

Does Java#39;s LinkedHashMap maintain the order of keys?(Java 的 LinkedHashMap 是否维护键的顺序?)

本文介绍了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.

这篇关于Java 的 LinkedHashMap 是否维护键的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java 的 LinkedHashMap 是否维护键的顺序?

基础教程推荐