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