0% found this document useful (0 votes)
12 views

Lets First See Why and When We Use Hashcode?

hashCode() is a method that is overridden in custom classes when equals() is overridden. Hashcode is used in hash-based collections like HashMap and HashSet. hashCode() returns a numeric value that is used to quickly retrieve objects, faster than using a string key. If multiple objects return the same hashCode value, equals() will be used to identify the correct object.

Uploaded by

NileshMehta28
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lets First See Why and When We Use Hashcode?

hashCode() is a method that is overridden in custom classes when equals() is overridden. Hashcode is used in hash-based collections like HashMap and HashSet. hashCode() returns a numeric value that is used to quickly retrieve objects, faster than using a string key. If multiple objects return the same hashCode value, equals() will be used to identify the correct object.

Uploaded by

NileshMehta28
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

hashCode() is a method that we override in our custom classes and its mandatory to override if the "equals" method is overridden.

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.

You might also like