I tried your code changing the objects we put into the map.It gave a diiferent hashCode() then. I think you got the same hashCode() coz hi.put("",""); hi.put("",""); here the objects we put are same.But How does it work?Coz the key cant be a duplicate one rite?
Hi I think it is the key whose hash code value really matters when dealing with maps, In your code Integer is the key and String are the values.Hash code increases the efficiency while retrieving values from the hashmap.Whenever one retrieves the values from hashmap the hashcode() is called on each key.If hash code is equal then equals method is called to check the values of key. In your code it is the hashcode of Integer method which really matters. Integer class has its own hashcode and equals method, so you don't need to override them . I don't know why you are calling hash code on hash map object. I am not sure but it may be calling hashcode method in Object class, which always returns distinct hash code.
ok hashCode() you called in you program belongs to class AbstractMap method has following specifications public int hashCode()
Returns the hash code value for this map. The hash code of a map is defined to be the sum of the hash codes of each entry in the map's entrySet() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.
This implementation iterates over entrySet(), calling hashCode on each element (entry) in the Collection, and adding up the results.
Map class is also having hashcode() method Regards Sunny Mattas