简体   繁体   中英

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

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.

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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