哈希码,等于合同,反之亦然 [英] hashcode and equals contract vice versa

查看:120
本文介绍了哈希码,等于合同,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道合同说如果两个对象相等,那么它们应该返回相同的哈希码".也就是说,那些对象可以放置在相同的哈希桶中,并提高与哈希代码相关的收集功能的了解.再说一遍为什么为什么说如果两个对象具有相同的哈希码,那么它们不应该总是相等的".我的意思是,如果合同中的内容是正确的,我们应该说如果两个对象相等,它们可能会返回相同的哈希码,但这不是强制性的"

解决方案

我的意思是,如果合同中的内容是正确的,我们应该说如果两个对象相等,它们可能会返回相同的值 哈希码,但不是强制性的"

否,不应该.这是因为,每当在HashMapHashSet中搜索对象时,然后首先根据hashCode进行搜索(注意:- hashCode()不是用于ArrayListLinkedList的搜索.它们不是hash based集合),然后,如果两个对象具有相同的哈希码,则移至equals方法以比较对象本身.

现在假设如果上面的语句为true,则对于这些对象,第一个测试本身将失败.也就是说,如果允许两个相等的对象具有不同的哈希码,则在搜索特定的hashCode时,它将不会返回正确的结果,因此测试将不会继续进行equals method,并将那些对象声明为unequal即使您希望它们相等.

现在让我们转到 second 语句:-

如果两个对象具有相同的哈希码,则它们不应该总是相等.

让我们这样理解:-由于为每个对象生成的hashCode类型为int,因此您可以生成最大的2 ^ 32唯一hashcodes.因此,想象一下,如果您想存储更多的对象,将会发生什么.在这种情况下,two different objects必须有 collison .因此,除了将相同的hashCodes分配给两个不同的对象外,您别无选择.因此,上述说法是合理的.


因此,从上面的解释中,很清楚两件事:-

  • 两个相同的对象必须具有相同的哈希码.
  • 两个不同的对象可以具有相同的hashCodes.

下面的链接中有关于此主题的更多详细信息(没有什么比这能提供更好的解释了):-

I know the contract says "if two objects are equal then they should return the same hash code". That is so that those objects can be place in the same hash bucket and to improve hash code related collection functions know. Then again why it says "if two objects have same hash code those should not always be equals". I mean if it is true in the contract we should say "if two objects are equals they may return same hash code but which is not mandatory"

解决方案

I mean if it is true in the contract we should say "if two objects are equals they may return same hash code but which is not mandatory"

No, it should not. This is because, whenever an object is searched let's say in a HashMap or a HashSet, then first it is searched on the basis of hashCode(NOTE : - hashCode() is not used for search in case of ArrayList, or LinkedList. They are not hash based collections), and then if the two objects have same hashcodes, it moves to the equals method to compare the objects themselves.

Now suppose if the above statement was true, the first test itself would fail for those objects. That is, if two equal objects are allowed to have different hashcode, then while searching for a particular hashCode, it won't return the correct result, and thus the test will not proceed to equals method, and declare those objects to be unequal even though you expected them to be equal.

Now lets move to the second statement: -

if two objects have same hash code those should not always be equals"

Let's understand like this: - Since hashCode generated for each object is of type int, so you can generate a maximum 2 ^ 32 unique hashcodes. So, imagine what would happen if you want to store more objects than that. In that case, there has to be a collison for two different objects. So, you would have no other way than to assign same hashCodes to two different objects. And hence the above statement is justified.


So, two things are quite clear from the above explanation: -

  • Two same objects must have same hashCodes.
  • Two different objects can have same hashCodes.

More details on this topic in the below link (Nothing can give better explanation than this): -

这篇关于哈希码,等于合同,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆