Is hashCode () used where?

Anyone use hashCode()


Can anyone give me an example of the exact use of hashcode and in what cases do we need to implement it?   Any specific area where HashCode is used?


source to share

3 answers

Is hascode used where?

The method is hashCode

used internally, for example, HashSet

or HashMap

, etc.

Does anyone give me an example of what exactly is used by hascode ...

It is used, for example, so that the algorithm can quickly detect if two objects are not equal. (Not comparing them with help equals


... and in what cases do we need to implement it?

You must implement it whenever you override equals

(which is what you need to do when you need to define two different objects so that they are equal).

Further reading



hashCode is used for example in hashmap. good implementation of the hashMap method allows you to have a good distribution of the data on the map and improve the performance of data access



I think you mean hashcode

, not hascode


It is hashcode

used to build structures like Maps

( HashMap


It is also used to store passwords sometimes, so basically a Java application passes the hashed version of the password to the database, which is then stored. This is useful if you have "leaks" between your Java application and the database, allowing people to see usernames and their passwords. Since you will be passing in a hashed version of the password, not the actual password, whoever is monitoring your application will make it very difficult to try to crack the password.

For someone to log in, all they have to do is compare the hash codes of the provided password and the one stored in the database.



All Articles