8

Is there any other reason for implementing an hash code function for my types other than allowing for good use of hash tables?

Let's say I am designing some types that I intend to use internally. I know that types are "internal" to the system, and I also know I will never use those types in hash tables. In spite of this, I decide I will have to redefine the equals() method.

Theory says I should also redefine the hash code method, but I can't see any reason why, in this case, I should do it.

Can anyone point me out any other reason?

This question can be rephrased to : in which situations should we implement a hash code method in our types.

PS : I am not asking how to implement one. I am asking when.

4 Answers 4

10

You might not - but will any of your code, for example, use LINQ? There are a number of unexpected places that might use a hashmap or dictionary on your data.

If you don't want unexpected... "fun", then if you change Equals, override GetHashCode. Likewise, any IEquatable<T>.Equals should match the object.Equals implementation.

0
6

Yes, definitely. hashCode and equals are 2 views on the same thing and have to be consistent. Many routines in the Collections use the hashcode and start misbehaving if it tells different things than equals. You can read 'misbehaving' as 'incredibly hard to find bugs which lead to early loss of hair'.

If you override Equals, you must Override hashcode, not because the guideline says so, but because you value your hair (or time).

Modern IDE's generate good equals/hashcode for you and the EqualsBuilder/HashCodeBuilder from Java Commons or Spring can help make it easier. Project Lombok generates them on-the fly.

This is serious stuff, and the best you can do with these methods is get it right, and there are hundreds of ways of doing it wrong, leading to pain and agony. If you can avoid writing them yourself, do so, use on of the generators or libraries to help you.

3
  • Resharper eats equality implementation for breakfast. Point, clickety click, go.
    – spender
    Jun 13, 2010 at 22:07
  • 1
    IntelliJ : Alt-Ins(Ctrl-N) + select "Generate Equals/Hashcode" with cursor + Enter. Eclipse and Netbeans similar. They have a drawback : if you use code metrics these routines stick out as sore thumbs due to cyclomatic complexity and not being able to follow all tracks through the codefor example. So they creat an environment where other nasty routines may hide. The EqualsBuilder/HashcodeBuilder do not have this problem. Jun 13, 2010 at 22:18
  • "... but because you value your hair (or time)". Or life, if a co-worker has to fix your mess. :-)
    – Stephen C
    Jun 13, 2010 at 23:53
4

in which situations should we implement a hash code method in our types.

Short answer: Whenever you override .Equals. Because the Framework Design Guidelines say so.

0

If you are really sure that your objects will not be used in a hashtable and that hashcode is not used, then you can override hashCode to throw a "not implemented" exception. This is safer than not overriding since it explicitly prohibits use of the method, and avoids the salient errors that would result otherwise. It will also show you clearly if the method is in fact used.

But, I would personally say just use the IDE to generate a resonable implementation that is consistent with Equals and be done with it. It's usually value types that override equals, and during testing of these, maps are typically used, so it makes sense to implement the full contract required by the equals/hashcode api.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.