Lets First See Why and When We Use Hashcode?
Lets First See Why and When We Use Hashcode?
Hashcode is also used in hashing based collection classes such as Hashtable, HashMap, HashSet etc. Understanding hashCode is very simple. Lets first see why and when we use hashCode? As we know, hashCode is a numeric value. It is used in retrieval of the respective object and it need not be unique which we see later. Now since its a number its always fast to retrieve an object using a number rather than an alphabetic key. How it will do? Assume we created a new object by passing some value which is already available in someother object. Now the new object will return the same hash value as of another object because the value passed is same. Once the same hash value is returned, JVM will go to the same memory address every time and if in case there are more than one objects present for the same hash value it will use equals() method to identify the correct object.