SEP
19
2005

What passes for grad work today... LOL


public int hashCode() {
/* This is probably a fairly bad hash function */
return 0;
}

Comments

In Java, it is a violation of contract for objects for which:

o1.equals(o2)
and:
o1.hashCode() != o2.hashCode()

But it is done all the time - how often do you see equals overriden without overriding the default hashcode, under which all objects are unique?

If you are just creating some objects that you know will be compared for equality, but you don't think they'll ever be hashed, returning a constant is a hackish but quick and not too inelegent way of keeping within the rules.


By jimmy_h at Mon, 09/19/2005 - 09:11