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
override
theequals
function as per rule we need to override thehashcode
function..? - So is that we implement the
hashcode
function along with equals method but thehascode
implementation is of no use..? And how the
hashcode
is used inhashmap
andhashtable
ofcollection
framework..?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,