The 3 things you should know about hashCode() « EclipseSource Blog
The 3 things you should know about hashCode() In Java, every object has a method hashCode that is simple to understand but still it’s sometimes forgotten or misused. Here are three things to keep in mind to avoid the common pitfalls. An object’s hash code allows algorithms and data structures to put objects into compartments, just like letter types in a printer’s type case. The printer puts all “A” types into the compartment for “A”, and he looks for an “A” only in this one compartment. This simple system lets him find types much faster than searching in an unsorted drawer. That’s also the idea of hash-based collections, such as HashMap and HashSet. The hashCode contract The contract is explained in the hashCode method’s JavaDoc. It can be roughly summarized with this statement: Objects that are equal must have the same hash code within a running process Please note that this does not imply the following common misconceptions: Unequal objects must have different hash codes – WRONG!Read full article from The 3 things you should know about hashCode() « EclipseSource Blog
No comments:
Post a Comment