简体   繁体   English

remove() 是否更改 LinkedHashMap 中的迭代顺序?

[英]Does remove() change iteration order in LinkedHashMap?

LinkedHashMap has underlying double-linked list, which enables preservation of the insertion order during the iteration. LinkedHashMap 底层有双链表,可以在迭代过程中保留插入顺序。 Non-structural changes, ie replacement of the value of an already inserted key, does not affect iteration order.非结构性更改,即替换已插入键的值,不会影响迭代顺序。 However, I am still wondering, whether remove(key) operation changes the iteration order in LinkedHashMap .但是,我仍然想知道, remove(key) 操作是否会更改 LinkedHashMap 中的迭代顺序 As I have tested on really small examples, it does not affect the order of the elements, except for the missing element, which is not included during iteration, obviously - but anecdotes are not proofs.正如我在非常小的例子上测试过的那样,它不会影响元素的顺序,除了缺少的元素,显然它在迭代期间不包括在内——但轶事不是证据。 Supposedly, removal works as if in LinkedList (where the halves of the list split are at the index of the element are joined together) - on the other hand, the programmer maybe should take into account rehashing or reallocation.据推测,删除工作就像在 LinkedList 中一样(其中列表拆分的两半在元素的索引处连接在一起) - 另一方面,程序员可能应该考虑重新散列或重新分配。

I am really confused and after reading the documentation of LinkedHashMap thoroughly, also still very doubtful, as I need a structure which preserves the order of insertion but enables an easy lookup and removal of the entries.我真的很困惑,在彻底阅读了 LinkedHashMap 的文档之后,我仍然非常怀疑,因为我需要一个结构来保留插入顺序但可以轻松查找和删除条目。

I am really confused and after reading the documentation of LinkedHashMap thoroughly, also still very doubtful...我真的很困惑,在仔细阅读了 LinkedHashMap 的文档之后,也仍然很怀疑......

Here's the relevant part of the specification .是规范的相关部分。

"This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map ( insertion-order ). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation." ) “此链表定义了迭代顺序,通常是将键插入 map的顺序(插入顺序)。请注意,如果将键重新插入 map,则插入顺序不受影响。(键k如果在调用m.containsKey(k)之前立即返回 true 时调用m.put(k, v)则将重新插入到 map m中。” )

The "normally" refers to the fact that you can create a LinkedHashMap in which the iteration order is access-order . “通常”是指您可以创建迭代顺序为访问顺序LinkedHashMap

That says nothing about the order of remaining entries being changed by deletion.这并没有说明通过删除更改剩余条目的顺序。 Indeed, if an implementation (hypothetically) did change the order when deleting an entry, that would plainly contradict the unqualified statements about iteration order.事实上,如果一个实现(假设地)在删除条目时确实改变了顺序,那将明显与关于迭代顺序的不合格陈述相矛盾 In other words, it would not conform to the specification.换句话说,它不符合规范。

Conversely, if the spec writers intended that deletion should affect the iteration order, they would have said so explicitly.相反,如果规范编写者打算删除应该影响迭代顺序,他们会明确说明。 Just like they did mentioned that "reinsertion" doesn't affect it.就像他们确实提到“重新插入”不会影响它一样。


As a general rule, specifications are not written to spell out precisely what happens in every possible scenario.作为一般规则,规范并不是为了准确说明在每种可能的情况下会发生什么。 If property Y is a logical consequence of a specified property X, then a specification does not need to (and typically won't) state property Y explicitly.如果属性 Y 是指定属性 X 的逻辑结果,则规范不需要(通常不会)明确地指定属性 Y state。

It is normal for specification writers to assume that readers will make the correct logical inferences for themselves.规范编写者假设读者会自己做出正确的逻辑推理是正常的。

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

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