1. public boolean equals(Object 0)
2. public int hashcode()
Both these methods have default implementation as part of the Object class which is inherited by all classes in java.
In the default implementation of equals method, a comparison is done on the memory address that the two objects point to. This means that only if the two variables point to the same object, the equals method will return true. However this is not always the desired mode of comparison. So Java allows developers to override this method for all classes.
In the default implementation of the hashcode method, the method returns the integer equivalent of memory location where the object is stored. There are 2 important rules that need to be taken care of while overriding the hashcode method.
1. The value of the hashcode cannot change once it is assigned. It needs to remain the same throughout the execution of the application. This is to ensure losing objects that are put in collection classes.
2. From a performance improvement perspective, 2 objects that are equal must have the same hashcode. Though the converse is not true - two objects with the same hashcode need not be equal.
Reason for overriding hashcode method when overriding equals method:
Consider the example of adding objects as keys to HashMap. While calling the put method of an HashMap with a key and value pair, a call is first made to the hashcode method of the key. This verifies if the object being added is a duplicate or not. If the key being added has the same hashcode as another object already present in the map, then the equals method is called to check for equality of the objects. If they do not have the same hashcode, then the key is immedaitely added to the map.
When we overwrite the equals method alone and leave the hashcode representation to be the default, there is a possibility that 2 equal objects(equal as defined by the custom method) will have different hashcodes. If we try to add 2 such objects to the map, we might end up creating duplicate entries in the map.
One possible way of overriding the hashcode method is to return a constant number for all objects. But this defeats the whole purpose of having a hashcode as everytime the hashcode is invoked to check for inquality, the map would goto the equals method to compare the objects(which is expensive).
It should also be noted that the hashcode can be made data dependant but however it needs to be taken care that it is assinged a value only once when the object is created and does not change everytime an object's data fields change. The reason can be explained with the example of adding two objects as keys in a HashMap. Consider an hashmap with two keys - obj1 with hashcode 10 and obj2 with hashcode 11. Now I modify the data in obj2 so that the data matches that of obj1 and the hashcode becomes 10. Now when I try to retrieve obj2 from the map, the map would return the value corresponding to key obj1. This leads to loss of data stored as part of obj2. Hence the value of hashcode needs to be assigned once and then everytime the hashcode method is invoked, this value needs to be retrieved rather than evaluating it again.
No comments:
Post a Comment