Is hashCode () used where?
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
- Better implementation for hashCode method
- Wikipedia: hash table (I highly recommend you read this)
source to share
I think you mean hashcode
, not hascode
...
It is hashcode
used to build structures like Maps
( HashMap
etc.).
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.
source to share