Can Java – hashcode () dynamically change content?

In my implementation, I have a Class A that overrides equals (object) and hashcode () But I have a small question, that is, when adding an instance of a to HashSet / HashMap, the value of hashcode () is x, and then the value of the same hashcode () is changed to y. what will it affect?

Solution

The hash code cannot be changed after it is added to the map / set It can be changed before, but it usually makes the type easier to use if it won't change

If the hash code changes, the key will not be found in the map / set, because even if it ends up in the same bucket, the hash code will be changed first

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
分享
二维码
< <上一篇
下一篇>>