开发者

Why does the Hashcode of an Object change in Java?

Why does the Hashcode of an Object change in Java? Does it change at all? How is it related to Hashtable ? Every开发者_高级运维 object should have it's unique hashcode.So, is Rehashing a reason for it ?

Thanks in advance.


The default implementation of hashcode is equivalent to object identity. However, some objects override hashcode, which might give you a hashcode that changes based on object state.

Usually you do this if you're overriding the definition of equals( in fact, if you override equals you should override hashcode). This is because you want objects that are equal by whatever definition you've created to return the same hashcode. Otherwise you can have a situation a map holds multiple "equal" objects, because they return different hashcodes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜