[英]Why don't Hashtables and dictionaries use Equals() method instead of GetHashCode for keys comparision in .NET?
In .NET, Whenever we override Equals() method for a class, it is a normal practice to override the GetHashCode() method as well. 在.NET中,每当我们为类重写Equals()方法时,通常也会覆盖GetHashCode()方法。 Doing so will ensure better performance when the object is used in Hashtables and Dictionaries.
这样做可以确保在Hashtables和Dictionaries中使用对象时获得更好的性能。 Two keys are considered to be equal in Hashtable only if their GetHashCode() values are same.
只有当它们的GetHashCode()值相同时,才会在Hashtable中将两个键视为相等。 My question is why can't the Hashtables use Equals() method to compare the keys?, that would have removed the burden of overriding GetHashCode() method.
我的问题是为什么Hashtables不能使用Equals()方法来比较密钥?,这将消除重写GetHashCode()方法的负担。
HastTable/Dictionaries use Equals
in case of collision (when two hash codes are same) . HastTable / Dictionaries在发生冲突时使用
Equals
(当两个哈希码相同时) 。
Why don't they use only
Equals
?他们为什么不使用唯一
Equals
?
Because that would require a lot more processing than accessing/ (comparing) integer value value (hash code). 因为这需要比访问/ (比较)整数值(哈希码)更多的处理。 (Since hash codes are used as index so they have the complexity of O(1))
(因为哈希码被用作索引,因此它们具有O(1)的复杂度)
A HashSet
(or HashTable
, or Dictionary
) uses an array of buckets to distribute the items, those buckets are indexed by the object's hash code (which should be immutable), so the search of the bucket the item is in is O(1). HashSet
(或HashTable
或Dictionary
)使用桶数组来分配项目,这些桶由对象的哈希码(应该是不可变的)索引,因此项目所在桶的搜索是O(1) 。
Then it uses Equals
within that bucket to find the exact match if there's more than one item with the same hashcode: that's O(N) since it needs to iterate over all items within that bucket to find the match. 然后,如果有多个具有相同哈希码的项目,则在该桶中使用
Equals
来查找完全匹配:那是O(N),因为它需要迭代该桶中的所有项目以找到匹配。
If a hashset used only Equals
, finding an item would be O(N) and you could aswell be using a list, or an array. 如果一个hashset只使用
Equals
,那么找到一个项目就是O(N),你也可以使用一个列表或一个数组。
That's also why two equal items must have the same hashcode, but two items with the same hashcode don't necessarily need to be equal. 这也是为什么两个相等的项必须具有相同的哈希码,但具有相同哈希码的两个项不一定需要相等。
Thus, for a given object instance, GetHashCode
needs to reflect the logic of Equals
, to some extent. 因此,对于给定的对象实例,
GetHashCode
需要在某种程度上反映Equals
的逻辑。
Now if you're overriding the Equals
method, you're providing custom comparison logic. 现在,如果您重写
Equals
方法,那么您将提供自定义比较逻辑。 As an example, let's say your custom comparison logic involves only one particular data member of the instance. 例如,假设您的自定义比较逻辑仅涉及实例的一个特定数据成员。 For a non-virtual
GetHashCode
method to be useful, it would have to be general enough to understand your custom Equals
logic and be able to come up with a custom hash code function (one that only involves your chosen data member) on the spot. 要使非虚拟
GetHashCode
方法有用,它必须足够通用,以便了解您的自定义Equals
逻辑,并能够在现场提供自定义哈希代码功能(仅涉及您选择的数据成员的功能)。
It's not that easy to write such a sophisticated GetHashCode
and it's not worth the trouble either, when the user can simply provide a custom one-liner that honors the initial requirement. 编写如此复杂的
GetHashCode
并不容易,当用户可以简单地提供符合初始要求的定制单行时,它也不值得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.