C#比较字典是否相等
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#比较字典是否相等相关的知识,希望对你有一定的参考价值。
如何最有效地比较两个字典的平等? - 除了强力迭代的键,尝试查找和比较值
var d1 = new Dictionary<string,int>();
d1.Add("a",2);
d1.Add("b",1);
d1.Add("c",1);
var d2 = new Dictionary<string,int>();
d2.Add("c",1);
d2.Add("b",1);
d2.Add("a",2);
如何最有效地比较平等?
不像这样的暴力:
static bool CompareDictionaries(Dictionary<string,int> d1, Dictionary<string,int> d2)
{
if (d1.Count != d2.Count) return false;
foreach(string key in d1.Keys)
{
if (!d2.ContainsKey(key)) return false;
if (d1[key] != d2[key]) return false;
}
return true;
}
答案
如果Dictionary键相同,则可以使用HashSet进行比较:
var set1 = new HashSet<int>(dictionary1.Values);
var set2 = new HashSet<int>(dictionary2.Values);
然后可以使用Union,Intersect,HopsSet公开的除外。此外,您可以使用此:
bool isEqual = set1.SetEquals(set2);
根据您的情况,基准测试是(HashSet vs Dictionary)here
另一答案
结合自定义SequenceEquals
扩展,您可以指定如何比较键和值,如下所示:
public static bool Equals<TKey, TValue>(IDictionary<TKey, TValue> a, IDictionary<TKey, TValue> b,
IComparer<TKey> keyComparer, IEqualityComparer<TValue> valueComparer)
{
if (ReferenceEquals(a, b))
return true;
if (b == null || a == null)
return false;
if (a.Count != b.Count)
return false;
var aOrdered = a.OrderBy(k => k.Key, keyComparer);
var bOrdered = b.OrderBy(k => k.Key, keyComparer);
return aOrdered.SequenceEqual(bOrdered, valueComparer);
}
延期:
public static bool SequenceEqual<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> first, IEnumerable<KeyValuePair<TKey, TValue>> second, IEqualityComparer<TValue> valueComparer)
{
if (valueComparer == null) valueComparer = EqualityComparer<TValue>.Default;
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
using (IEnumerator<KeyValuePair<TKey, TValue>> e1 = first.GetEnumerator())
using (IEnumerator<KeyValuePair<TKey, TValue>> e2 = second.GetEnumerator())
{
while (e1.MoveNext())
{
if (!(e2.MoveNext() && valueComparer.Equals(e1.Current.Value, e2.Current.Value))) return false;
}
if (e2.MoveNext()) return false;
}
return true;
}
您可以在OP的特定Dictionary<string,int>
示例中调用它:
var areEqual = Equals(d1, d2, StringComparer.OrdinalIgnoreCase, EqualityComparer<int>.Default);
以上是关于C#比较字典是否相等的主要内容,如果未能解决你的问题,请参考以下文章