The hashCode()
and equals()
are two methods of java.lang.Object
class. We know by creating a class, java internally extend the class with java.lang.Object
class so that all the methods on Object class become available for all java classes. hashCode()
and equals()
are two methods of Object class
This article we are mainly discussing on the hasCode()
and equals()
methods from basic to advanced view
What is the use of equals() method?
The ultimate use of the equals method is for comparison. with the help of equals()
method, it able to compare an object with another object. Let's do a comparison of two strings.
So in the above example, it is created two string objects s1 and s2 with value Anish. then it is calling the equals() method of the s1 object by passing the s2 object. the String
class override the equals method and the overridden method doing the comparison of the current string and the string instance coming as the parameter. If both of the values are equal then the method returns true boolean value else it returns false.
Here is the implementation of the equals method of the String object
Here what it does is, It is converting both strings into a character array and comparing each charactors in a while loop. If both character array is the same then the method is returning true else false
Comparing custom created object
Now, let's find out how to do the comparison of custom created objects.
Output false
In the above example it is created two instances of Contact
object with the same name and phone number and calling the equals method of c1 Contact object but it returns false. Here you can note in the Contact object there is no equals methods are overridden so what happens is it is calling the java.lang.Object
classes equal method. The equals method of java.lang.Object
method looks like follows
Here it is comparing the object with == operator. the "==" operator compares the value of two object references to see whether they refer to the same Contact instance. But in the above example, we can see that the two variables c1 and c2 refer separate instance of the Contact
object, that's why the above program equals returns false as the result.
Let's override the equals method on Contact class and apply the logic something like if phone number and name are equal then we can say that both of the objects are equal. Here is the updated code snippet
Hope you got a good understanding about equals()
method. Now let's go to the hashCode()
method
What is hashcode?
hashCode() is a method provided by java.lang.Object which returns an integer representation of the object memory address. By default, this method returns a random integer which is unique for each instance, this integer might change between several executions of the application and wouldn’t stay the same.
In Object class, the hashCode method is a native method and equals method is an object method
Let's move to the above example and try to print out the hashcode of both contact object
So here while calling the c1.hashCode()
and c2.hashCode()
it is actually invoking the hashCode()
method of java.lang.Object
class and that method is a native method.
It indicates that hashCode is the native implementation which provides the memory address to a certain extent.
What is the actual use of hashcode?
hashCode()
is used for bucketing in Hash implementations like HashMap, HashTable, HashSet, etc.
The value received from hashCode()
is used as the bucket number for storing elements of the set/map. This bucket number is the address of the element inside the set/map.
When you do contains()
it will take the hash code of the element, then look for the bucket where hash code points to. If more than 1 element is found in the same bucket (multiple objects can have the same hash code), then it uses the equals()
method to evaluate if the objects are equal, and then decide if contains()
is true or false, or decide if element could be added in the set or not.
Let's go back to the ContactBook Example. Here you can see that it inserting the same Contact object into the HashSet . Here it returns true if you compare the Contact instance with the equal operator. Also, we know that the set interface won't allow duplicate object. If trying to insert a duplicate object into a set then it was overwritten the existing object.
But here it returns the size of the HashSet interface is 2 means set cannot able to identify both objects are equals. The reason for this is, it is not overrided the hashCode method in the contact object. When the system trying to save a new contact object into the hash set first it will look into the hashcode of the given object, if the hashcodes are equal then only it is proceeds with the equals method. But here the hascode generation done by the default Object.hashcode() method. So it will generate seperate hashcode for the both contact object. So the system treat both contact object as different
Why HashCode?
We use hashcode to perform some operation on hashing related algorithm like a hashtable, hashmap etc..the advantages of hashcode is it makes searching operation easy bcoz when we search for an object that unique code helps to find out the corresponding bucket. Then we need to only check the corresponding bucket each element. So that we can skip the equal comparison of each and every element of the corresponding collection
How to create HashCode?
the user can override the hashcode()
method and he can able to implement his on logic to the corresponding method. If return just a constant from hashCode()
function then also it should work. But there we are not taking the advantage of hashCode()
implementation and it is a bit wrong implementation
The Objects
class support generation of hashcode, Here follows an example for that
No comments:
Post a Comment