使用异或的 GetHashCode() 问题
我的理解是,您通常应该将 xor 与 GetHashCode() 一起使用来生成 int,以通过其值(而不是通过其引用)来识别数据。 这是一个简单的例子:
class Foo
{
int m_a;
int m_b;
public int A
{
get { return m_a; }
set { m_a = value; }
}
public int B
{
get { return m_b; }
set { m_b = value; }
}
public Foo(int a, int b)
{
m_a = a;
m_b = b;
}
public override int GetHashCode()
{
return A ^ B;
}
public override bool Equals(object obj)
{
return this.GetHashCode() == obj.GetHashCode();
}
}
我的想法是,我想根据属性 A 和 B 的值将 Foo 的一个实例与另一个实例进行比较。如果 Foo1.A == Foo2.A 且 Foo1.B == Foo2.B,那么我们有平等。
问题如下:
Foo one = new Foo(1, 2);
Foo two = new Foo(2, 1);
if (one.Equals(two)) { ... } // This is true!
这两个函数都会为 GetHashCode() 生成值 3,从而导致 Equals() 返回 true。 显然,这是一个简单的示例,只有两个属性,我可以简单地比较 Equals() 方法中的各个属性。 然而,对于更复杂的类,这很快就会失控。
我知道有时只设置一次哈希码并始终返回相同的值是很有意义的。 然而,对于需要评估相等性的可变对象,我认为这是不合理的。
在实现 GetHashCode() 时处理可以轻松互换的属性值的最佳方法是什么?
另请参阅
什么是最佳算法重写 System.Object.GetHashCode?
My understanding is that you're typically supposed to use xor with GetHashCode() to produce an int to identify your data by its value (as opposed to by its reference). Here's a simple example:
class Foo
{
int m_a;
int m_b;
public int A
{
get { return m_a; }
set { m_a = value; }
}
public int B
{
get { return m_b; }
set { m_b = value; }
}
public Foo(int a, int b)
{
m_a = a;
m_b = b;
}
public override int GetHashCode()
{
return A ^ B;
}
public override bool Equals(object obj)
{
return this.GetHashCode() == obj.GetHashCode();
}
}
The idea being, I want to compare one instance of Foo to another based on the value of properties A and B. If Foo1.A == Foo2.A and Foo1.B == Foo2.B, then we have equality.
Here's the problem:
Foo one = new Foo(1, 2);
Foo two = new Foo(2, 1);
if (one.Equals(two)) { ... } // This is true!
These both produce a value of 3 for GetHashCode(), causing Equals() to return true. Obviously, this is a trivial example, and with only two properties I could simply compare the individual properties in the Equals() method. However, with a more complex class this would get out of hand quickly.
I know that sometimes it makes good sense to set the hash code only once, and always return the same value. However, for mutable objects where an evaluation of equality is necessary, I don't think this is reasonable.
What's the best way to handle property values that could easily be interchanged when implementing GetHashCode()?
See Also
What is the best algorithm for an overridden System.Object.GetHashCode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先 - 不要仅根据 GetHashCode() 实现 Equals() - 即使对象不相等,哈希码有时也会发生冲突。
GetHashCode() 的契约包括以下内容:
Andrew Hare 建议我合并他的答案:
我会推荐您阅读了此解决方案(顺便说一句,由我们自己的 Jon Skeet 编写),以获取“更好”的计算哈希码的方法。
正如上面的代码片段中提到的,您可能还想看看Joshua Bloch 的书,Effective Java, 其中包含对该主题的很好的处理(哈希码讨论也适用于 .NET)。
First off - Do not implement Equals() only in terms of GetHashCode() - hashcodes will sometimes collide even when objects are not equal.
The contract for GetHashCode() includes the following:
Andrew Hare suggested I incorporate his answer:
I would recommend that you read this solution (by our very own Jon Skeet, by the way) for a "better" way to calculate a hashcode.
As mentioned in the above snippet, you might also want to look at Joshua Bloch's book, Effective Java, which contains a nice treatment of the subject (the hashcode discussion applies to .NET as well).
Andrew 发布了一个生成更好的哈希代码的好示例,但也要记住,您不应该使用哈希代码作为相等性检查,因为它们不能保证是唯一的。
举一个简单的例子来说明为什么这被认为是一个双重对象。 它比 int 有更多可能的值,因此不可能为每个 double 都有一个唯一的 int。 哈希实际上只是第一遍,用于像字典这样的情况,当您需要快速找到密钥时,通过首先比较哈希,可以排除很大一部分可能的密钥,并且只有具有匹配哈希的密钥才需要花费完全相等检查(或其他冲突解决方法)。
Andrew has posted a good example for generating a better hash code, but also bear in mind that you shouldn't use hash codes as an equality check, since they are not guaranteed to be unique.
For a trivial example of why this is consider a double object. It has more possible values than an int so it is impossible to have a unique int for each double. Hashes are really just a first pass, used in situations like a dictionary when you need to find the key quickly, by first comparing hashes a large percentage of the possible keys can be ruled out and only the keys with matching hashes need to have the expense of a full equality check (or other collision resolution methods).
散列总是涉及冲突,你必须处理它(例如,比较散列值,如果它们相等,则精确比较类内的值以确保类相等)。
使用简单的异或,您会遇到很多冲突。 如果您想要更少,请使用一些数学函数将值分布在不同的位上(移位、与素数相乘等)。
Hashing always involves collisions and you have to deal with it (f.e., compare hash values and if they are equal, exactly compare the values inside the classes to be sure the classes are equal).
Using a simple XOR, you'll get many collisions. If you want less, use some mathematical functions that distribute values across different bits (bit shifts, multiplying with primes etc.).
阅读重写可变对象的 GetHashCode? C# 并考虑实现
IEquatable
Read Overriding GetHashCode for mutable objects? C# and think about implementing
IEquatable<T>
有几种更好的哈希实现。 例如 FNV 哈希。
There are several better hash implementations. FNV hash for example.
出于好奇,因为哈希码通常不是比较的好主意,所以只执行以下代码不是更好吗?还是我遗漏了一些东西?
Out of curiosity since hashcodes are typically a bad idea for comparison, wouldn't it be better to just do the following code, or am I missing something?
哈希的快速生成和良好分布
A quick generate and good distribution of hash