本文介绍了是否保证从LinkedHashMap对象返回键和值的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我知道 LinkedHashMap 具有可预测的迭代顺序(插入顺序)。 LinkedHashMap.keySet()返回的 Set 和 Collection 由 LinkedHashMap.values()返回还维护此订单?解决方案 Map接口提供三个 集合视图,它允许将地图的内容视为一组的键,值集合,或设置键值映射的。 地图的顺序定义为地图集合视图上的迭代器返回其元素的顺序。一些映射的实现,比如 TreeMap 类,对它们的订单作出的特定保证;其他的,比如 HashMap 类,不要。 - 地图 此链接列表定义了迭代的顺序,通常是订单,其中键被插入到地图中( 插入订单)。 - LinkedHashMap 所以,是的, 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 the HashMap 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).-- LinkedHashMapSo, 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对象返回键和值的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-05 10:47