hashCode() - how to implement it? Where is it used?

Method hashCode() is inherited from Object class. Its purpose is to return an  int value, which can be used to represent given object. There is a contract between  hashCode() and equals() which says that whenever equals() method used to compare two objects returns true, then values returned by hashCode() functions called on those objects must be the same. Remember that it does not work like that vice versa -  when hashCode() values are the same, then it does not mean that equals() invoked for those two objects will always return true. hashCode() method is widely used by java containers like HashMap and HashSet. That is why it is desired that hashCode() will return unique values for unique objects as often as it is possible since it can improve performace of these containers (however it is not concidered as an error when hashCode() returns the same value for different objects).

How to implement it?

Whenever you override an equals()method, you will also have to override hashCode() method to fulfill the contract between them (when equals() used to compare two objects returns true, then values returned by hashCode() for those objects must be the same).
The basic implementation of  hashCode() uses object address as returned value. The conclusion is - whenever you create your own class, and you want hashCode() to return unique values for unique objects as often as it is possible, then you will have to implement it. You will see that in the example below.

Lets create a Car class:


Now lets compare hashCode() values. The cars have the same content (fields) so we would expect hashCode() values of both car objects to be the same, since these objects are not unique.  


Unfortunatelly, hashCode() methods are returning different values. Basic implementation inherited from Object class leads to comparison of  object addresses, which are not the same, so we receive false value. It breaks the contract with equals() method which returns true in this case. 

To fix this issue we have to implement hashCode() method in our Car class. To do that properly we have to take into consideration all of the fields in the class. IntelliJ, Eclipse and NetBeans have their methods to auto-generate hashCode() methods. Below is the example generated with IntelliJ :

Now hashCode() invoked for the same Car instances will return the same values.


When should we override it?

Method hashCode() is used in multiple data structures e.g. HashSet, HashMap (a separate article on that is here: HashMap - how it works?). This data structures base their implementation on assumption, that elements inside data structure have a proper implementation of hashCode() which enables HashSet and HashMap to detect the same objects. Wrong implementation of hashCode() or using base implementation will lead to incorrect bahaviour of those data structures.

Komentarze

Popularne posty z tego bloga

Spring Data 1# - how repositories work under the hood

Hibernate 1# - entity states overview

Data consistency and performance in web applciation