T
Tom Dyess
This is gonna be a tough question to put into words, but here goes:
I understand the purpose of overriding equals() in comparing the data of an
object verses the reference, but the requirement to override hashCode() is a
little less clear.
Consider this idiom from this reference:
http://www.geocities.com/technofundo/tech/java/equalhash.html
"Equal objects must produce the same hash code as long as they are equal,
however unequal objects need not produce distinct hash codes."
The simplest way to create an equals and hashCode implementation for an
object would be to make a distinct hash code based on the value of the data
in the object, then in equals(), simply compare the value of hash codes. Why
would I not want to make a distinct hashCode based on the data - i.e.
"however unequal objects need not produce distinct hash codes"
Thanks for any information. I'm researching this because we are using
Hibernate in our project and want to understand how it can apply.
I understand the purpose of overriding equals() in comparing the data of an
object verses the reference, but the requirement to override hashCode() is a
little less clear.
Consider this idiom from this reference:
http://www.geocities.com/technofundo/tech/java/equalhash.html
"Equal objects must produce the same hash code as long as they are equal,
however unequal objects need not produce distinct hash codes."
The simplest way to create an equals and hashCode implementation for an
object would be to make a distinct hash code based on the value of the data
in the object, then in equals(), simply compare the value of hash codes. Why
would I not want to make a distinct hashCode based on the data - i.e.
"however unequal objects need not produce distinct hash codes"
Thanks for any information. I'm researching this because we are using
Hibernate in our project and want to understand how it can apply.