Is java) hashCode() called automatically?

Asked 2 years ago, Updated 2 years ago, 24 views

I need to use hashCode(), equals().

Does HashMap automatically call when saving both functions?

And except for this framework, when comparing objects equally,

Do I have to call and use both functions directly?

android java

2022-09-20 18:07

2 Answers

Yes, the implementation method of hashmap is to index with the code value of "hasCode(). Also, since hashing itself is a different object, but it can return the same hascode, finally we call "equals()" to make sure it is the same object.

And you don't have to call both functions to make an equal comparison. You just need to call the "equals()" function to see if it's equivalent, but it depends on how you implement the "equals." If you did not override the "equals" function, return true only when it is the same reference object. It depends on what class you're trying to determine if it's a real equivalent object. If you want to make sure that the class you created is equivalent, and you want to make sure that the actual data is the same, not the same reference object, you need to override the "equals" function.

And if you want to use hashmap with the class you created, you also need to override the hashcode() function. If you don't know how to override it, search for it and you can refer to the blogs that others wrote down in detail.


2022-09-20 18:07

Oh, thank you! The hash map is called automatically, and I have to call it myself among other ordinary objects.


2022-09-20 18:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.