我怎样才能生成字符串一个独特的哈希码 [英] How can I generate a unique hashcode for a string

查看:116
本文介绍了我怎样才能生成字符串一个独特的哈希码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么功能,这让我对相同的字符串相同的hashCode?

创建2个不同的字符串时,我有麻烦(但具有相同的内容),它们的哈希码是不同的,因此不正确地在词典



使用

我想知道是什么的GetHashCode()功能词典使用时,最关键的是一个字符串。



我建我的是这样的:

 公共覆盖INT的GetHashCode()
{
字符串str =装备+ Equipment.ToString()+命运+ Destiny.ToString();
返回str.GetHashCode();
}



但它生产用于使用此代码的每个实例不同的结果,尽管内容串是相同的。


解决方案

您题目要求一件事(的唯一的散列码)你的身体会要求不同的东西(的相一致的散列码)



您声称:



< BLOCKQUOTE>

创建2个不同的字符串时,我有麻烦(但具有相同的内容),它们的哈希码是不同的,因此不正确地在字典中。


< /块引用>

如果字符串的真正的有相同的内容,即根本不会发生。你的诊断是错误的莫名其妙。检查你的字符串,例如非打印字符结尾的Unicode空字:

 字符串的text1 =你好; 
字符串文本2 =Hello\0;

下面文本1 文本2 可以打印同样的方式在某些情况下,但我希望他们就会有不同的散列码。



需要注意的是哈希码是的的保证是唯一的和的无法的是...只有2 32 可能的哈希码从返回的GetHashCode ,但超过2 32 可能不同的字符串。



另外请注意,同一内容的的保证产生甚至同一个可执行在不同的运行相同的散列码, - 你不应该的持续的哈希代码的任何地方。例如,我相信32位.NET 4和64位.NET 4的CLR生成的字符串不同的散列码。但是,您的索赔,该值未正确储存在词典表明,这是一个单一的过程中 - 这里的一切的的是一致的



正如评论指出,这是完全有可能的,你重写等于不正确。我还建议你的方法来构建一个哈希码是不是很大。我们不知道是什么类型的设备命运是的,但我建议你应该使用类似

 公共覆盖INT的GetHashCode()
{
INT哈希= 23;
散列=散列* 31 + Equipment.GetHashCode();
散列=散列* 31 + Destiny.GetHashCode();
返回哈希;
}

这是我通常使用的哈希码的方法。 等于则看起来是这样的:

 公共覆盖布尔等于(对象其他)
{
//参考平等检查
如果(此==其他)
{
返回真;
}
如果(其他== NULL)
{
返回false;这种
}
//详细信息可能会改变根据您的情况;我们最好
//需要了解更多信息
如果(other.GetType()=的GetType()!)
{
返回FALSE;
}

//调整你的类型...
美孚otherFoo =(美孚)等;

//你可能需要基础上,
//类型的设备和命运
返回this.Destiny == otherFoo.Destiny和放大器的改变这里使用的平等;&安培;
this.Equipment == otherFoo.Equipment;
}


Is there any function, that gives me the same hashcode for the same string?

I'm having trouble when creating 2 different strings (but with the same content), their hashcode is different and therefore is not correctly used in a Dictionary.

I would like to know what GetHashCode() function the Dictionary uses when the key is a string.

I'm building mine like this:

public override int GetHashCode()
{
   String str = "Equip" + Equipment.ToString() + "Destiny" + Destiny.ToString();
   return str.GetHashCode();
}

But it's producing different results for every instance that uses this code, despite the content of the string being the same.

解决方案

Your title asks for one thing (unique hash codes) your body asks for something different (consistent hash codes).

You claim:

I'm having trouble when creating 2 different strings (but with the same content), their hashcode is different and therefore is not correctly used in a Dictionary.

If the strings genuinely have the same content, that simply won't occur. Your diagnostics are wrong somehow. Check for non-printable characters in your strings, e.g trailing Unicode "null" characters:

string text1 = "Hello";
string text2 = "Hello\0";

Here text1 and text2 may print the same way in some contexts, but I'd hope they'd have different hash codes.

Note that hash codes are not guaranteed to be unique and can't be... there are only 232 possible hash codes returned from GetHashCode, but more than 232 possible different strings.

Also note that the same content is not guaranteed to produce the same hash code on different runs, even of the same executable - you should not be persisting a hash code anywhere. For example, I believe the 32-bit .NET 4 and 64-bit .NET 4 CLRs produce different hash codes for strings. However, your claim that the values aren't being stored correctly in a Dictionary suggests that this is within a single process - where everything should be consistent.

As noted in comments, it's entirely possible that you're overriding Equals incorrectly. I'd also suggest that your approach to building a hash code isn't great. We don't know what the types of Equipment and Destiny are, but I'd suggest you should use something like:

public override int GetHashCode()
{
    int hash = 23;
    hash = hash * 31 + Equipment.GetHashCode();
    hash = hash * 31 + Destiny.GetHashCode();
    return hash;
}

That's the approach I usually use for hash codes. Equals would then look something like:

public override bool Equals(object other)
{
    // Reference equality check
    if (this == other)
    {
        return true;
    }         
    if (other == null)
    {
        return false;
    }
    // Details of this might change depending on your situation; we'd
    // need more information
    if (other.GetType() != GetType())
    {
        return false;
    }

    // Adjust for your type...
    Foo otherFoo = (Foo) other;

    // You may want to change the equality used here based on the
    // types of Equipment and Destiny
    return this.Destiny == otherFoo.Destiny &&
           this.Equipment == otherFoo.Equipment;
}

这篇关于我怎样才能生成字符串一个独特的哈希码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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