是否保证从LinkedHashMap对象返回键和值的顺序? [英] Is the order guaranteed for the return of keys and values from a LinkedHashMap object?
问题描述
我知道 LinkedHashMap
具有可预测的迭代顺序(插入顺序)。 LinkedHashMap.keySet()
返回的 Set
和 Collection
由 LinkedHashMap.values()
返回还维护此订单?
< blockquote>
Map接口提供三个
集合视图,它允许将地图的内容视为一组
的键,值集合,或设置键值映射的
。
地图的顺序定义为地图集合
视图上的迭代器
返回其元素的顺序。一些映射
的实现,比如 TreeMap
类,对它们的订单作出
的特定保证;其他的,比如
HashMap
类,不要。
- 地图
此链接列表定义了迭代
的顺序,通常是订单
,其中键被插入到
地图中( 插入订单)。
所以,是的, keySet()
, values()
和 entrySet()
(提到的三个集合视图)按内部链表使用的顺序返回值。是的,JavaDoc for Map
和 LinkedHashMap
保证它。
毕竟,这就是本课程的重点。
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?
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.
-- Map
This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).
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对象返回键和值的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!