Java 是否保证从 LinkedHashMap 对象返回键和值的顺序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2923856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:33:13  来源:igfitidea点击:

Is the order guaranteed for the return of keys and values from a LinkedHashMap object?

javaiterationlinkedhashmap

提问by user256239

I know LinkedHashMaphas a predictable iteration order (insertion order). Does the Setreturned by LinkedHashMap.keySet()and the Collectionreturned by LinkedHashMap.values()also maintain this order?

我知道LinkedHashMap有一个可预测的迭代顺序(插入顺序)。是否Set通过返回LinkedHashMap.keySet()Collection返回的LinkedHashMap.values()也维持这种秩序?

采纳答案by Powerlord

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 orderof 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 TreeMapclass, make specific guarantees as to their order; others, like the HashMapclass, do not.

Map 接口提供了三个 集合视图,允许将地图的内容视为一组键、一组值或一组键值映射。该订单的地图被定义为其中在地图上的集合视图迭代返回元素的顺序。一些映射实现,如TreeMap类,对它们的顺序做出特定保证;其他人,就像 HashMap班级一样,没有。

-- Map

--地图

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order).

这个链表定义了迭代顺序,通常是将键插入到映射中的顺序插入顺序)。

-- LinkedHashMap

-- LinkedHashMap

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 Mapand LinkedHashMapguarantee it.

所以,是的,keySet()values(),和entrySet()顺序(三组收集的意见中提到)返回值的内部链接列表的用途。是的,JavaDoc 用于MapLinkedHashMap保证它。

That is the point of this class, after all.

毕竟,这就是这门课的重点。

回答by extraneon

Looking at the interface it returns a plain Setand not an SortedSet. So there are no guarantees.

查看界面,它返回一个普通的Set而不是SortedSet. 所以没有任何保证。

Before assuming an implicit guarantee by looking at the implementation (always a bad idea) also look at the implementations in all other Java implementations :)

在通过查看实现(总是一个坏主意)来假设隐式保证之前,还要查看所有其他 Java 实现中的实现 :)

You could better create for instance a TreeSet with the keySet in the constructor.

例如,您最好在构造函数中使用 keySet 创建一个 TreeSet 。

回答by sblundy

Looking at the source, it looks like it does. keySet(), values(), and entrySet()all use the same entry iterator internally.

看源码,好像是这样。keySet(), values(), 和entrySet()都在内部使用相同的入口迭代器。

回答by Uri

AFAIK it is not documented so you cannot "formally" assume so. It is unlikely, however, that the current implementation would change.

AFAIK 它没有记录,所以你不能“正式”假设。然而,目前的实施不太可能改变。

If you want to ensure order, you may want to iterate over the map entires and insert them into a sorted set with an order function of your choice, though you will be paying a performance cost, naturally.

如果您想确保顺序,您可能需要遍历整个地图,并将它们插入到一个带有您选择的 order 函数的有序集合中,尽管您自然会付出性能成本。

回答by user207421

You can assume so. The Javadoc says 'predictable iteration order', and the only iterators available in a Map arethose for the keySet(), entrySet(), and values().

你可以这样假设。Javadoc 说“可预测的迭代顺序”,Map唯一可用的迭代器那些用于 keySet()、entrySet() 和 values() 的迭代器。

So in the absence of any further qualification it is clearly intended to apply to all of those iterators.

因此,在没有任何进一步限定的情况下,它显然打算应用于所有这些迭代器。

回答by Zoro

I don't think you can presume the ordering of keySet() and values().

我认为您不能假设 keySet() 和 values() 的顺序。

I can easily write an implementation of LinkedHashMap that returns you unordered keySet() and values(), as long as I stick to the contract of these two methods that are defined in Map, and overridden in HashMap.

我可以很容易地编写一个 LinkedHashMap 的实现,它返回无序的 keySet() 和 values(),只要我坚持这两个在 Map 中定义并在 HashMap 中覆盖的方法的约定。

回答by anzaan

Don't get confused with LinkedHashMap.keySet()and LinkedHashMap.entrySet()returning Set and hence it should not guarantee ordering !

不要混淆LinkedHashMap.keySet()LinkedHashMap.entrySet()返回 Set ,因此它不应该保证订购!

Setis an interface with HashSet,TreeSetetc beings its implementations. The HashSetimplementation of Setinterface does not guarantees ordering. But TreeSetdoes. Also LinkedHashSetdoes.

Set是一个接口HashSetTreeSet等等是它的实现。接口的HashSet实现Set不保证排序。但TreeSet确实如此。也LinkedHashSet可以。

Therefore it depends on how Sethas been implemented in LinkedHashMapto know whether the returning Set reference will guarantee ordering or not. I went through the source code of LinkedHashMap, it looks like this:

因此,它取决于如何Set实现 inLinkedHashMap来知道返回的 Set 引用是否能保证排序。我浏览了 的源代码LinkedHashMap,它看起来像这样:

private final class KeySet extends AbstractSet<K> {...}
public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {...}

Thus LinkedHashMap/HashMap has its own implementation of Seti.e. KeySet. Thus don't confuse this with HashSet.

因此 LinkedHashMap/HashMap 有它自己的Setie实现KeySet。因此,不要将此与HashSet.

Also, the order is maintained by how the elements are inserted into the bucket. Look at the addEntry(..)method of LinkedHashMapand compare it with that of HashMapwhich highlights the main difference between HashMapand LinkedHashMap.

此外,顺序由元素插入存储桶的方式维护。查看 的addEntry(..)方法LinkedHashMap并将其与HashMap突出显示HashMap和之间主要区别的方法进行比较LinkedHashMap