Is the order guaranteed for the return of keys and values from a LinkedHashMap object?(从 LinkedHashMap 对象返回键和值的顺序是否得到保证?)
问题描述
我知道 LinkedHashMap
有一个可预测的迭代顺序(插入顺序).LinkedHashMap.keySet()
返回的Set
和LinkedHashMap.values()
返回的Collection
是否也维护这个顺序?
I know LinkedHashMap
has a predictable iteration order (insertion order). Does the Set
returned by LinkedHashMap.keySet()
and the Collection
returned by LinkedHashMap.values()
also maintain this order?
推荐答案
Map 接口提供了三个集合视图,允许将地图的内容视为一个集合键、值的集合或集合键值映射.顺序地图被定义为其中的顺序地图集合上的迭代器视图返回它们的元素.一些地图实现,例如
TreeMap
类,作出具体保证他们的订单;其他人,比如HashMap
类,不要.
The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the
TreeMap
class, make specific guarantees as to their order; others, like theHashMap
class, do not.
-- 地图
这个链表定义了迭代排序,通常是顺序其中钥匙被插入地图(插入顺序).
This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
-- LinkedHashMap
所以,是的,keySet()
、values()
和 entrySet()
(提到的三个集合视图)在内部链表使用的顺序.是的,Map
和 LinkedHashMap
的 JavaDoc 保证了这一点.
So, yes, keySet()
, values()
, and entrySet()
(the three collection views mentioned) return values in the order the internal linked list uses. And yes, the JavaDoc for Map
and LinkedHashMap
guarantee it.
毕竟这就是这门课的意义所在.
That is the point of this class, after all.
这篇关于从 LinkedHashMap 对象返回键和值的顺序是否得到保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 LinkedHashMap 对象返回键和值的顺序是否得到保证?


- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01