I was wondering whether the .Net HashSet<T>
is based completely on hash codes or whether it uses equality as well?
我想知道.Net HashSet
I have a particular class that I may potentially instantiate millions of instances of and there is a reasonable chance that some hash codes will collide at that point.
我有一个特定的类,我可能会实例化数百万个实例,并且有一些合理的可能性,即某些哈希码将在该点发生冲突。
I'm considering using HashSet's to store some instances of this class and am wondering if it's actually worth doing - if the uniqueness of an element is only determined on its hash code then that's of no use to me for real applications
我正在考虑使用HashSet来存储这个类的一些实例,我想知道它是否真的值得做 - 如果一个元素的唯一性只是在它的哈希码上确定那么对我来说对于实际的应用程序没用
MSDN documentation seems to be rather vague on this topic - any enlightenment would be appreciated
MSDN文档在这个主题上似乎相当含糊 - 任何启示都会受到赞赏
1 个解决方案
#1
14
No, it uses equality as well. By definition, hash codes don't need to be unique - anything which assumes they will be is broken. HashSet<T>
is sensible. It uses an IEqualityComparer<T>
(defaulting to EqualityComparer<T>.Default
) to perform both hash code generation and equality tests.
不,它也使用平等。根据定义,哈希码不需要是唯一的 - 假定它们将被破坏的任何东西。 HashSet
#1
14
No, it uses equality as well. By definition, hash codes don't need to be unique - anything which assumes they will be is broken. HashSet<T>
is sensible. It uses an IEqualityComparer<T>
(defaulting to EqualityComparer<T>.Default
) to perform both hash code generation and equality tests.
不,它也使用平等。根据定义,哈希码不需要是唯一的 - 假定它们将被破坏的任何东西。 HashSet