最好的办法比较两个词典< T>平等 [英] Best way to compare two Dictionary<T> for equality

查看:122
本文介绍了最好的办法比较两个词典< T>平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是创建两个字典的平等比较器的最佳方式?这需要非常精确。需要注意的是Entity.Columns是KeyValuePair(字符串对象)的字典:

Is this the best way to create a comparer for the equality of two dictionaries? This needs to be exact. Note that Entity.Columns is a dictionary of KeyValuePair(string, object) :

public class EntityColumnCompare : IEqualityComparer<Entity>
{
    public bool Equals(Entity a, Entity b)
    {
        var aCol = a.Columns.OrderBy(KeyValuePair => KeyValuePair.Key);
        var bCol = b.Columns.OrderBy(KeyValuePAir => KeyValuePAir.Key); 

        if (aCol.SequenceEqual(bCol))
            return true;
        else
            return false;           
    }

    public int GetHashCode(Entity obj)
    {
        return obj.Columns.GetHashCode(); 
    }
}



也不太清楚有关的GetHashCode的实现。

Also not too sure about the GetHashCode implementation.

感谢

推荐答案

下面是我会做:

    public bool Equals(Entity a, Entity b)
    {
        if (a.Columns.Count != b.Columns.Count)
            return false; // Different number of items

        foreach(var kvp in a.Columns)
        {
            object bValue;
            if (!b.Columns.TryGetValue(kvp.Key, out bValue))
                return false; // key missing in b
            if (!Equals(kvp.Value, bValue))
                return false; // value is different
        }
        return true;
    }



这种方式,您不需要命令项(这是一个为O(n log n)的的操作):你只需要枚举在第一词典中的词条(的 O(N)的),并尝试在第二个字典检索键值( O(1)的),所以整体复杂性的 O(N)

That way you don't need to order the entries (which is a O(n log n) operation) : you only need to enumerate the entries in the first dictionary (O(n)) and try to retrieve values by key in the second dictionary (O(1)), so the overall complexity is O(n).

另外,还要注意你的的GetHashCode 方法不正确:在大多数情况下,它会返回不同的字典实例不同的价值观,即使他们有相同的内容。而如果哈希码不同的是,等于将永远不会被调用......您有几种选择正确实现它,他们没有理想的:

Also, note that your GetHashCode method is incorrect: in most cases it will return different values for different dictionary instances, even if they have the same content. And if the hashcode is different, Equals will never be called... You have several options to implement it correctly, none of them ideal:


  • 从字典的内容建设的哈希码:会是最好的选择,但它是缓慢的,而的GetHashCode 需求要快

  • 总是返回相同的值,这样,等于总是被称为:的非常的坏如果你想在一个哈希表/字典/ HashSet的使用这个比较器,因为所有的实例会落在同一个桶,造成的 O(N)的访问,而不是的 O(1)

  • 返回计数词典(由digEmAll建议):它不会给一个很大的分布,但仍比总是返回相同的值,它满足了的GetHashCode 约束(即被认为是相等的对象必须有相同的hashCode;两个平等的字典具有相同数量项目,所以它的工作原理)

  • build the hashcode from the content of the dictionary: would be the best option, but it's slow, and GetHashCode needs to be fast
  • always return the same value, that way Equals will always be called: very bad if you want to use this comparer in a hashtable/dictionary/hashset, because all instances will fall in the same bucket, resulting in O(n) access instead of O(1)
  • return the Count of the dictionary (as suggested by digEmAll): it won't give a great distribution, but still better than always returning the same value, and it satisfies the constraint for GetHashCode (i.e. objects that are considered equal should have the same hashcode; two "equal" dictionaries have the same number of items, so it works)

这篇关于最好的办法比较两个词典&LT; T&GT;平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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