• Post Reply Bookmark Topic Watch Topic
  • New Topic

Query on HashMap

 
ms raaghu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15759
74
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need a Map that keeps elements in the order you inserted them, use LinkedHashMap instead of HashMap.
 
ms raaghu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15759
74
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.



 
ms raaghu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!