This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Not sure, what made you to think it is sorted. In your code, the values are printed in the order of system.out.println(). You overwrote the value of key m1 and therefore the latest value of m1 is being printed.
Originally posted by Shiv Mohan (i) just meant,based on the keys (referencevariable m1,m2,m3) that order ,values are sorted.
1- You have not overridden equals() and hashCode() methods of the Obejct class in your class named MyClass. It is recommended that when you are using your own class as a key, you should give definition to both the methods to compare when two objects should be treated equals. What default implementation of the equals() says, two objects are equal if their reference variable are referring to the same object on the heap.
So far as your confusion of ordering is concerned: there is no magic with get() method: get method takes the object reference(key) and returns the value, you confusion is running around the same; you may be thinking that get is returning the objects in the same way you have stored. But it is nothing like that, you can try by changing the order:
There is nothing like order in the HashMap. And no order means no sorting of course.