hashCode function conflict
It says hashCode must keep returning the same value for non modified object. But the standart hash algorithm based on random numbers returns different values.Why?
And one more similar question, custom classes i use, contains function equals , that doesnt use hashCode to compare objects. It only used in hash maps. Why its not neccesary just to compare objects by its hash code, its much nore simplier than anithing else.
Re: hashCode function conflict
Quote:
Originally Posted by
voipp
But the standart hash algorithm based on random numbers returns different values.Why?
Page and paragraph please; none of the correctly implemented hashCode() methods do that.
kind regards,
Jos
Re: hashCode function conflict
You'll have to give us some code that shows the hash value changing for an unmodified object, because I suspect you are wrong.
I'm not too sure what you're asking for the second bit, but it sounds like you think a hashcode is sufficient to determine if two objects are equal?
In which case I would point out that a hashing algorithm does not guarantee uniqueness.
Re: hashCode function conflict
Quote:
Originally Posted by
voipp
Why its not neccesary just to compare objects by its hash code, its much nore simplier than anithing else.
A hash code is some value based on information from an object which can be used to categorize or group the object with other similar objects. By similar, I mean that have the same hash code. So the last 4 digits of a phone number is a legitimate (but not necessarily good) hash code. Yet the phone numbers in that group could still be different.
Regards,
Jim
Re: hashCode function conflict
You mean because of collisions , objects with equal hash code could not be compared by equals() function ?
Re: hashCode function conflict
I don't know how you get that from what Jim said.
Re: hashCode function conflict
from this phrase - Quote:
A hash code is some value based on information from an object which can be used to categorize or group the object with other similar objects. By similar, I mean that have the same hash code
Perhaps , someone ought to speak more clearly. English is not my mother tongue, so , please keep answering more clearly or write your posts in Russian
Re: hashCode function conflict
Quote:
Originally Posted by
voipp
You mean because of collisions , objects with equal hash code could not be compared by equals() function ?
No. Collisions are irrelevant in this case. The rules are that two objects which have the same hashCode may or may not be equal as determined by the
equals() method. But if the objects are equal as determine by the equals() method, then the hashCodes must be the same. Otherwise, the objects won't play well with maps.
Regards,
Jim
Re: hashCode function conflict