Java – why is it necessary to override hashcode and equals methods of HashMap?

See English answers > what issues should be considered when overriding equals and hashcode in Java? 11

Now I've read everywhere that it's necessary to override the equals and hashcode methods if they are used as keys for HashMap

But I didn't cover it And insert multiple key value pairs into HashMap I can get it back, too

Then why is it necessary?

Solution

They are required when you want to use objects as useful hash keys in collections that use hashes

Are you trying to add a student object with meaningful equality? First, understand the significance of equals method in Java

Read significance of equals and hashcode

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>