Possible Duplicate:
Hashcode and equals
I have read many articles about hashcode and equals and their relations ship.
So far my understanding is every object has equals and hashcode function by default .As java class has those functions.
Now hashcode means it returns the memory address of an object.hashcodes are unique by default .When a object is created you will get a unique has code.
- Now my question is when you
overridetheequalsfunction as per rule we need to override thehashcodefunction..? - So is that we implement the
hashcodefunction along with equals method but thehascodeimplementation is of no use..? And how the
hashcodeis used inhashmapandhashtableofcollectionframework..?class person{ string name; string employer; boolean equals(Object o){ person per=(person)o; if(per.employer==this.employeer){ return true } return false; } int hashcode(){ return 0;//what ever i do in hashcode does it really effect any thing..as the equals does //the comparison for me and gave me the result } }
and how the hascodes are used in hashmap and hashtable
Thanks,