比较两个 Dictionary的最佳方法为了平等
这是创建两个字典相等的比较器的最佳方法吗?这需要准确。请注意,Entity.Columns 是 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 实现。
谢谢!
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();
}
}
Also not too sure about the GetHashCode implementation.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我要做的:
这样你就不需要对条目进行排序(这是一个O(n log n)操作):你只需要枚举第一个字典中的条目(< em>O(n)) 并尝试通过第二个字典中的键检索值 (O(1)),因此整体复杂度为 O(n)嗯>。
另请注意,您的
GetHashCode
方法不正确:在大多数情况下,即使它们具有相同的内容,它也会为不同的字典实例返回不同的值。如果哈希码不同,Equals
将永远不会被调用...您有多种选择来正确实现它,但没有一个是理想的:GetHashCode
需要很快,Equals
将始终被调用:非常如果您想在哈希表/字典/哈希集中使用此比较器,因为所有实例都将落入同一个存储桶中,从而导致 O(n) 访问而不是 O(1)Count
(按照 digEmAll 的建议):它不会给出很好的分布,但仍然比总是返回相同的值要好,并且它满足GetHashCode 的约束
(即被认为相等的对象应该具有相同的哈希码;两个“相等”的字典具有相同数量的项目,因此它可以工作)Here's what I would do:
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).
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
needs to be fastEquals
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)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 forGetHashCode
(i.e. objects that are considered equal should have the same hashcode; two "equal" dictionaries have the same number of items, so it works)我想到了类似的东西,但可能还有更有效的东西:
Something like this comes to mind, but there might be something more efficient:
对我来说这似乎很好,也许不是最快的,但很有效。
您只需更改错误的
GetHashCode
实现即可。例如,您可以返回 obj.Columns.Count.GetHashCode()
It seems good to me, perhaps not the fastest but working.
You just need to change the
GetHashCode
implementation that is wrong.For example you could return
obj.Columns.Count.GetHashCode()