Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

hashmap

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.

like image 314
Watt Avatar asked Feb 04 '13 13:02

Watt


People also ask

Why order is not maintained in HashMap?

” HashMap does not preserve insertion order “. HashMap is collection of Key and Value but HashMap does not give guaranty that insertion order will preserve. i.e here we are adding data of student result from 1st to 3rd year but when we retrieve its there are possibility to change sequence.

Is order guaranteed in HashMap?

HashMap is unordered per the second line of the documentation: This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

Is HashMap always constant time?

Whatever may be the size of the data, HashMap almost gives constant time performance for most frequent operations – insertion and retrieval. That's why HashMap is the first choice for the big sized data having requirement of faster retrieval and faster insertion operations.

Does HashMap maintain sorted order?

Java HashMap does not preserve any order by default. If there is a need to sort HashMap we sort it explicitly based on the requirements. Java provides an option to sort HashMap based on keys and values. In this section, we will learn how to sort HashMap according to keys and values.


2 Answers

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

like image 100
Jean Logeart Avatar answered Sep 18 '22 07:09

Jean Logeart


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

like image 36
Peter Lawrey Avatar answered Sep 22 '22 07:09

Peter Lawrey