Why it is necessary to override hashcode and equals method of key for Hashmap? [duplicate]

我只是一个虾纸丫 提交于 2019-12-10 14:35:14

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/18606447/why-it-is-necessary-to-override-hashcode-and-equals-method-of-key-for-hashmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!