2

I noticed in examples of hashCode(), everyone is using the same fields to call hashCode() inside the hashCode() method definition.

Why should I have to call/initialize the hashCode() method for each member of a class, as hash code is for the finding the object location (correct me if i am wrong)?

So what is the purpose of calling the hashCode() inside the hashCode() method definition.

For example:

public int hashCode() {
    final int seed = 37;
    int result = 1;
    result = seed * result + ((name == null) ? 0 : name.hashCode());
    result = seed * result + age;
    result = seed * result + marks;
    return result;
}

Here we have two fields age and name. What is the purpose of name.hashCode() on line no 4.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • See also: https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode-- reusing the hashCode is common practice because it will follow the contract for that field. But nobody forces you to do so. `age` is probably an int - so you cannot call hashCode() on it ... – Fildor Jan 17 '17 at 14:32
  • Ok. i got the point. But my question is why??? means why we need to call hashCode() over the non primitive members? – MANOHAR SINGH RAGHUWANSHI Jan 17 '17 at 16:46

2 Answers2

1

I think a more generic form of this question has already been asked -

Best implementation for hashCode method

To summarise it for your question, "name" is a String object and "age" is an int literal. The seed value is usually a prime number.

Community
  • 1
  • 1
1

The hashCode method is used by collections like HashMap and HashSet to distribute your object's instances in a way that they will be fast to retrieve (with a time complexity of O(1)). The better is your hashCode method implemented, the more efficient would be those collections when used with your object's instances. A well implemented hashCode method would limit as much as possible the probability to get hash collisions, in other words, it would limit the possibility to get the same hash code for 2 different object's instances.

Knowing that, if we remove the line #4 from your hashCode's implementation, we would then have all the object's instances with the same age and marks that would have the same resulting hash code whatever the value of name so the probability to get hash collisions is high which is not wanted as explained above.

So since hashCode returns a 32 bits integer and name is obviously a String (at least not a primitive type), we call hashCode() on it to get an int representation of the String in order to add somehow its value to the resulting hash code. Thanks to that, 2 object's instances with the same age and marks won't have necessarily the same hash code, the risk of getting hash collisions is limited which is what we expect.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • 1
    Thank you so much @Nicolas Filtto This is what i want to know. i ll never forget the reason for using hashCode() for non primitive type. your answer is very helpful to understand.!! – MANOHAR SINGH RAGHUWANSHI Jan 17 '17 at 16:53