简体   繁体   English

为什么HashMap不保证地图的顺序会随着时间的推移保持不变

[英]Why HashMap does not guarantee that the order of the map will remain constant over time

I was reading about difference between Hashmap and Hashtable here: http://javarevisited.blogspot.sg/2010/10/difference-between-hashmap-and.html 我在这里阅读Hashmap和Hashtable之间的区别: http ://javarevisited.blogspot.sg/2010/10/difference-between-hashmap-and.html

Can anyone throw some light on why it says following? 任何人都可以说明为什么它会跟随?

"5. HashMap does not guarantee that the order of the map will remain constant over time." “5. HashMap不保证地图的顺序会随时间保持不变。”

Could the order change during re-hashing, that is why? 在重新散列期间订单是否会发生变化,这就是为什么?

It would be also nice if you could point me to resource or list of collections who exhibit such behavior of not guaranteeing order to be remain constant. 如果你能指出资源或集合列表,表现出不保证秩序保持不变的行为,那也很好。

AFIK, ArrayList gives such gurantee (let me know if I am wrong) AFIK,ArrayList提供了这样的保证(如果我错了,请告诉我)

EDIT: 'Order of map' = maybe order in which keys or values are entered. 编辑:'地图顺序'=可能是输入键或值的顺序。

A HashMap has no order - at any time. HashMap在任何时候都没有订单。 It is actually not used for that purpose. 它实际上并不用于此目的。 The order may change even when not rehashing. 即使没有重复,订单也可能会改变。

If you need the order to remain constant, use a LinkedHashMap 如果您需要使订单保持不变,请使用LinkedHashMap

The point of a hashing strategy is to place objects in a pseudo random manner. 散列策略的要点是以伪随机方式放置对象。 It does this so that most of the time, only one key/element will be hashed to a given bucket. 它这样做是为了在大多数情况下,只有一个键/元素将被散列到给定的桶。 This allows an O(1) lookup time. 这允许O(1)查找时间。 When a HashMap or Hashtable grows, the number of buckets changes and the keys/elements are placed in another pseudo random manner. 当HashMap或Hashtable增长时,桶的数量发生变化,键/元素以另一种伪随机方式放置。

The simplest solution to this is to use LinkedHashMap. 最简单的解决方案是使用LinkedHashMap。 This will keep order of addition or optionally order of last access. 这将保持添加顺序或可选的上次访问顺序。 I prefer to use this collection because it makes debugging easier as I can predict where an object is likely to be and sometimes the order an object was added can be useful information. 我更喜欢使用这个集合,因为它可以使调试变得更容易,因为我可以预测对象可能在哪里,有时添加对象的顺序可能是有用的信息。

BTW If you are interested in how many orders a small number of keys can have Order of elements in a hash collection BTW如果您对多少订单感兴趣,那么少量密钥可以在哈希集合中包含元素顺序

For me the following code: 对我来说,代码如下:

Map <Integer, Object> map = new HashMap <Integer, Object> (4);

map.put (60, null);
map.put (48, null);
map.put (29, null);

System.out.println (map);

map.put (47, null);
map.put (15, null);
map.put (53, null);

map.remove (47);
map.remove (15);
map.remove (53);

System.out.println (map);

outputs: 输出:

{29=null, 48=null, 60=null}
{48=null, 29=null, 60=null}

The order changed because of the re-hashing that happens when the initial capacity (here, 4) is exceeded. 由于在超过初始容量(此处为4)时发生的重新散列,订单发生了变化。 Even though the additional entries are removed again, the original order isn't restored. 即使再次删除其他条目,也不会恢复原始订单。

A HashMap has number of buckets (implemented as an array) in which to store entries. HashMap具有多个存储条目的存储桶(实现为数组)。

When an item is added to the map, it is assigned to a buckets based on a value derived of its hashCode and the bucket size of the HashMap. 将项添加到地图时,会根据其hashCode的值和HashMap的存储区大小将其分配给存储桶。 (Note that it's possible that the bucket is already occupied, which is called a collision. That's handled gracefully and correctly, but I'll ignore that handling for the description because it doesn't change the concept). (请注意,存储桶可能已被占用,这称为冲突。这是优雅且正确的处理,但我会忽略对描述的处理,因为它不会改变概念)。

Why HashMap does not guarantee that the order of the map will remain constant over time 为什么HashMap不保证地图的顺序会随着时间的推移保持不变

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

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