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

浪子不回头ぞ 提交于 2019-12-10 03:32:18

问题


I was reading about difference between Hashmap and Hashtable here: 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."

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)

EDIT: 'Order of map' = maybe order in which keys or values are entered.


回答1:


A HashMap has no order - at any time. 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




回答2:


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. When a HashMap or Hashtable grows, the number of buckets changes and the keys/elements are placed in another pseudo random manner.

The simplest solution to this is to use 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




回答3:


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. Even though the additional entries are removed again, the original order isn't restored.




回答4:


A HashMap has number of buckets (implemented as an array) in which to store entries.

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. (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



来源:https://stackoverflow.com/questions/14687406/why-hashmap-does-not-guarantee-that-the-order-of-the-map-will-remain-constant-ov

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!