I created a HashMap having Student
as key and String as value.
Now everywhere i have read It is necessary to override equals
and hashcode
method if using as a key for hashmap.
But I did not override it. And insert multiple key value pairs in hashmap. I am also able to fetch it back.
So why it is necessary?
They are required when you want to use your objects as useful hashing keys into collections that use hashing.
Have your tried to add the Student
objects which are meaningfully equal?
First understand the significance of equals method in java
Read the significance of equals and hashcode
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With