简体   繁体   English

为什么Hashtables和词典不使用Equals()方法而不是GetHashCode用于.NET中的键比较?

[英]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 (或HashTableDictionary )使用桶数组来分配项目,这些桶由对象的哈希码(应该是不可变的)索引,因此项目所在桶的搜索是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.

相关问题 在个人类上使用字典时,为什么不必覆盖GetHashCode? - Why don't I ever have to override GetHashCode when using Dictionaries on personal classes? 不要使用Type.GetMethods()返回ToString,Equals,GetHashCode,GetType - Don't return ToString, Equals, GetHashCode, GetType with Type.GetMethods() 为什么在重写 Equals 方法时重写 GetHashCode 很重要? - Why is it important to override GetHashCode when Equals method is overridden? 为什么没有 Equals() 的 GetHashCode() 没有警告 - Why no warning for GetHashCode() without Equals() 为什么IEqualityComparer <T> 有GetHashCode()方法? - Why IEqualityComparer<T> has GetHashCode() method? 使用“external”GetHashCode和Equals for Dictionary - Use “external” GetHashCode and Equals for Dictionary 为什么不列出 <T> .GetHashCode和ObservableCollection <T> .GetHashCode评估他们的项目? - why don't List<T>.GetHashCode and ObservableCollection<T>.GetHashCode evaluate their items? Dictionary <T>如何处理未实现Equals和GetHashCode的关键对象? - How does Dictionary<T> work with key objects that don't implement Equals and GetHashCode? &#39;不要暴露通用列表&#39;,为什么要使用集合 <T> 而不是列表 <T> 在方法参数中 - 'Don't expose generic list', why to use collection<T> instead of list<T> in method parameter 字典的单声道实现 <T,T> 使用.Equals(obj o)而不是.GetHashCode() - Mono implementation of Dictionary<T,T> using .Equals(obj o) instead of .GetHashCode()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM