哈希code在字典< TKEY的,TValue> [英] Hash code in Dictionary<TKey, TValue>

查看:111
本文介绍了哈希code在字典< TKEY的,TValue>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是玩弄字典和整个以下情形

I was playing around with Dictionary and stumbled across the below scenario

public class MyObject
{
    public string I { get; set; }
    public string J { get; set; }
    public string K { get; set; }

    public override int GetHashCode()
    {
        int hashCode = (I+J+K).GetHashCode();
        Debugger.Log(9, "INFO", hashCode.ToString() + System.Environment.NewLine);
        return hashCode;
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyObject obj1 = new MyObject() { I = "Hello", J = "World" };
        MyObject obj2 = new MyObject() { I = "Hello", J = "World" };

        Dictionary<MyObject, string> collection = new Dictionary<MyObject, string>();
        collection.Add(obj1, "1");
        var result = collection[obj2]; // KeyNotFound exception here.
    }
}

我的MyObject类,它充当一个关键字典和我重写GetHash code方法返回基于存储在类的哈希值code。

I have MyObject class which acts as a key to dictionary and I override the GetHashCode method to return hash code based on the values stored in the class.

所以,执行上述code时,两种OBJ1和OBJ2返回相同的哈希code,但字典引发KeyNotFound例外。

So, when the above code is executed, both obj1 and obj2 returns same hash code, but still the dictionary throws KeyNotFound exception.

有什么理由如此的行为呢?

Any reason why such a behavior?

推荐答案

在.NET中, GetHash code 用在演唱会与等于方法来判断对象的相等就存储在集合。

In .NET, GetHashCode is used in concert with the Equals method to determine object equality with regard to storage in collections.

请注意,一个哈希表是不是简单地通过散列code映射键来单槽更加复杂。由于散列codeS的性质,碰撞可发生和确实的出现在实践中(虽然具有良好的哈希函数这不应该是经常)。因此,大多数的哈希表实现具有处理两个不同的对象生成相同的哈希code的情况下,这通常在散列表中的每个槽实现了与一个链表。散列code为用于确定槽和等于方法被用于确定下落对象存储在该链接的表(在大多数标准的实施哈希表)。

Note that a hash table is more complex than simply mapping a key to a single slot via a hash code. Due to the nature of hash codes, collisions can occur and do occur in practice (though with a good hash function this should not be very often). Thus most hash table implementations have to deal with the case of two different objects generating the same hash code and this is often achieved with a linked list at each "slot" in the hash table. The hash code is used to determine the slot and the Equals method is used to determine whereabouts the object is stored in the linked list (in most "standard" implementations of a hash table).

一个字的警告,但是:很少有很好的理由来覆盖 GetHash code 的内置行为。我发现了这个有趣的SO线程讨论 GetHash code 等于这应该是值得一读:<一href="http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethash$c$c-when-equals-method-is-overriden-in-c">http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethash$c$c-when-equals-method-is-overriden-in-c.它讨论改变行为的好处/坏处,好的和差的Hash函数的性质,这两种方法和其他好吃的东西所需要的属性。

A word of warning, however: there are very few good reasons to override the built-in behaviour of GetHashCode. I found this interesting SO thread discussing GetHashCode and Equals that should be worth a read: http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c. It discusses the merit/demerits of changing the behaviour, the properties of good and bad hash functions, the required properties of these two methods and other goodies.

这篇关于哈希code在字典&LT; TKEY的,TValue&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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