是否保证从 LinkedHashMap 对象返回键和值的顺序? [英] Is the order guaranteed for the return of keys and values from a LinkedHashMap object?

查看:51
本文介绍了是否保证从 LinkedHashMap 对象返回键和值的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 LinkedHashMap 有一个可预测的迭代顺序(插入顺序).LinkedHashMap.keySet()返回的SetLinkedHashMap.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 the HashMap 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()(提到的三个集合视图)在内部链表使用的顺序.是的,MapLinkedHashMap 的 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 对象返回键和值的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆