For a class whose fields are solely primitive, ex.:
class Foo
{
int a;
String b;
boolean c;
long d;
boolean equals(Object o)
{
if (this == o) return true;
if (!(o instanceof Foo)) return false;
Foo other = (Foo) o;
return a == other.a && b.equals(other.b) && c == other.c && d = other.d;
}
}
Is this a reasonably "good enough" way to write hashCode()
?
boolean hashCode()
{
return (b + a + c + d).hashCode();
}
That is, I construct a String
out of the same fields that equals()
uses, and then just use String#hashCode()
.
Edit: I've updated my question to include a long
field. How should a long
be handled in hashCode()
? Just let it overflow int
?