Simberon Design Minute
 

Equals and Hash

There are many developers who will happily code their own equals method for their classes but not realize that they also need to implement hash. The hash method returns an Integer that matches the equals method. In other words, if two objects are considered equal, then their hash values must be the same. If the objects aren't equal, the hash values may or may not be the same. I'm not going to talk here about how to get a good hash value but I will say that if you have an equals method, you really should have a hash method as well. If you don't, you'll have problems if you try dropping your objects into a set or use them as keys in a dictionary. If you define equals, you should always define a hash that matches it.

Download