What’s a correct and good way to implement __hash__()?

What’s a correct and good way to implement __hash__()? I am talking about the function that returns a hashcode that is then used to insert objects into hashtables aka dictionaries. As __hash__() returns an integer and is used for “binning” objects into hashtables I assume that the values of the returned integer should be uniformly … Read more

How to get the unique ID of an object which overrides hashCode()?

When a class in Java doesn’t override hashCode(), printing an instance of this class gives a nice unique number. The Javadoc of Object says about hashCode(): As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. But when the class overrides hashCode(), how can I … Read more

What issues should be considered when overriding equals and hashCode in Java?

This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. What issues / pitfalls must be considered when overriding equals and hashCode? 1Best Answer 11 The theory (for the language lawyers and the mathematically inclined): equals() (javadoc) must define an equivalence relation … Read more

What is the best algorithm for overriding GetHashCode?

In .NET, the GetHashCode method is used in a lot of places throughout the .NET base class libraries. Implementing it properly is especially important to find items quickly in a collection or when determining equality. Is there a standard algorithm or best practice on how to implement GetHashCode for my custom classes so I don’t … Read more