简体   繁体   English

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

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

I know LinkedHashMap has a predictable iteration order (insertion order). 我知道LinkedHashMap具有可预测的迭代顺序(插入顺序)。 Does the Set returned by LinkedHashMap.keySet() and the Collection returned by LinkedHashMap.values() also maintain this order? 是否Set由归国LinkedHashMap.keySet()Collection返回由LinkedHashMap.values()也维持这种秩序?

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. Map界面提供了三个集合视图 ,这些视图允许将地图的内容作为一组键,一组值或一组键-值映射来查看。 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; 一些地图实现(例如TreeMap类)对其顺序做出特定的保证。 others, like the HashMap class, do not. 其他的(例如HashMap类)则没有。

-- Map - 地图

This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map ( insertion-order ). 此链表定义了迭代顺序,通常是将键插入映射中的顺序insert-order )。

-- LinkedHashMap -LinkedHashMap

So, yes, keySet() , values() , and entrySet() (the three collection views mentioned) return values in the order the internal linked list uses. 因此,是的, keySet()values()entrySet() (提到的三个集合视图)按内部链表使用的顺序返回值。 And yes, the JavaDoc for Map and LinkedHashMap guarantee it. 是的,JavaDoc for MapLinkedHashMap保证了这一点。

That is the point of this class, after all. 毕竟,这就是本课的重点。

Looking at the source, it looks like it does. 从源头看,看起来确实如此。 keySet() , values() , and entrySet() all use the same entry iterator internally. keySet()values()entrySet()都在内部使用相同的条目迭代器。

Don't get confused with LinkedHashMap.keySet() and LinkedHashMap.entrySet() returning Set and hence it should not guarantee ordering ! 不要与LinkedHashMap.keySet()LinkedHashMap.entrySet()返回Set混淆,因此它不保证排序!

Set is an interface with HashSet , TreeSet etc beings its implementations. Set是与HashSetTreeSet等实现的接口。 The HashSet implementation of Set interface does not guarantees ordering. Set接口的HashSet实现不保证排序。 But TreeSet does. 但是TreeSet确实如此。 Also LinkedHashSet does. LinkedHashSet也可以。

Therefore it depends on how Set has been implemented in LinkedHashMap to know whether the returning Set reference will guarantee ordering or not. 因此,这取决于在LinkedHashMap如何实现Set才能知道返回的Set引用是否可以保证排序。 I went through the source code of LinkedHashMap , it looks like this: 我查看了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 Set ie KeySet . 因此,LinkedHashMap / HashMap具有自己的Set实现,即KeySet Thus don't confuse this with HashSet . 因此,请勿将此与HashSet混淆。

Also, the order is maintained by how the elements are inserted into the bucket. 同样,通过将元素插入存储桶的方式来保持顺序。 Look at the addEntry(..) method of LinkedHashMap and compare it with that of HashMap which highlights the main difference between HashMap and LinkedHashMap . 查看LinkedHashMapaddEntry(..)方法,并将其与HashMap方法进行比较,从而突出显示HashMapLinkedHashMap之间的主要区别。

You can assume so. 您可以这样假设。 The Javadoc says 'predictable iteration order', and the only iterators available in a Map are those 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. 因此,在没有任何进一步限定的情况下,显然打算将其应用于所有这些迭代器。

AFAIK it is not documented so you cannot "formally" assume so. AFAIK没有记录,因此您不能“正式”假设如此。 It is unlikely, however, that the current implementation would change. 但是,当前的实施方式不太可能会改变。

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. 如果您想确保顺序,则可能要遍历整个地图,然后将其插入具有所选顺序功能的排序集中,尽管您自然会支付性能费用。

Looking at the interface it returns a plain Set and not an SortedSet . 查看接口,它返回一个普通Set而不是SortedSet So there are no guarantees. 因此,没有任何保证。

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。

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. 只要我坚持Map中定义并在HashMap中覆盖的这两个方法的协定,就可以轻松编写LinkedHashMap的实现,该实现返回无序的keySet()和values()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM