Forums Register Login

Query on HashMap

+Pie Number of slices to send: Send
Hi ,

I have a doubt in the below simple code , the output printed here is Before {A=A}
After {P=B, A=A}

But if I change the key from say "P" to "Z" then the output is Before {A=A}
After {A=A, Z=B}

My question is :why is that the output changes for a key P ,i.e. it reverses the order in which its inserted?



+Pie Number of slices to send: Send
If you read the docs for the Map interface, you'll see that it says it makes no guarantees about iteration order.

For HashMap, the order is probably a combination of the second hash of the hashCode (which bucket it goes in) and the order in which it was inserted (where in that bucket it lies). Since it's not easy to predict which bucket a given key will end up in, we can't easily predict what the iteration order will be for a given set of insertions.
+Pie Number of slices to send: Send
If you need a Map that keeps elements in the order you inserted them, use LinkedHashMap instead of HashMap.
+Pie Number of slices to send: Send
 

Jeff Verdegan wrote:If you read the docs for the Map interface, you'll see that it says it makes no guarantees about iteration order.


i agree that there is no particular order but it happens only when the keys are "P" ,"T" and "Q", not for others. Any specific reason behind this?
+Pie Number of slices to send: Send
 

ms raaghu wrote:i agree that there is no particular order but it happens only when the keys are "P" ,"T" and "Q", not for others. Any specific reason behind this?



No there is no specific difference for P,T and Q. Read this Tutorial. It will help.
+Pie Number of slices to send: Send
 

ms raaghu wrote:i agree that there is no particular order but it happens only when the keys are "P" ,"T" and "Q", not for others. Any specific reason behind this?


You should see HashMap as a bag of things, where each thing has a label (the key) attached to it.

Suppose you want to look at what's in the bag. You stick you hand in the bag, grab the first thing that your hand touches, and look at it. Then you stick in your hand again, etc. Note that in what order you take the things out of the bag is random. A bag doesn't hold things in any particular order, therefore there's no reason to be surprised when things come out of the bag in any particular order.

In practice, the order in which a HashMap shows its contents depends on implementation details of class HashMap. If you really want to know, you could dig into the source code of class HashMap to find out. But it's not really important - the only thing that's important is to know that HashMap does not make any guarantees about the order of its elements.
+Pie Number of slices to send: Send
 

ms raaghu wrote:it happens only when the keys are "P" ,"T" and "Q", not for others.



This is probably false. You probably just haven't discovered other combinations yet.

And even if it is true, it's just coincidence. If you want to understand why, you can look at the code for HashMap and work through it yourself. It's in src.zip in the JDK download.



+Pie Number of slices to send: Send
Thank you all for clear cut explanation , i got it clear how map works ... and for my purpose am using LInkedHashMap for the same...

Thanks again
Shiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1818 times.
Similar Threads
equal() & hashcode()
switch default
Static values don't get serialized, yet the code seems to show the contrary
Struggling with generics, "unchecked call to put()"
XML handler --> populate Hashmap
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:56:18.