我知道该HashSet<T>.SetEquals方法,但是何时以及如何CreateSetComparer使用?
该文档指出:“仅在一个级别检查相等性;但是,您可以将其他级别的比较器链接在一起以执行更深入的相等性测试”
一个简单的例子是什么?
特别是,如果我要比较的集合中的每个项目也包含 HashSet ,那么 的正确用法是什么CreateSetComparer?
这是我的出发点。我想知道该CreateSetComparer方法是否适用以及如何正确使用它:
public class Foo : IEquatable<Foo>
{
public string Label { get; set; }
public string Value { get; set; }
public override string ToString() {return String.Format("{0}:{1}", Label, Value); }
// assume for this example that Label and Value are immutable once set;
public override int GetHashCode(){ return ToString().GetHashCode(); }
// simplified equality check; assume it meets my needs for this example;
public bool Equals(Foo other){ return String.Equals(this.ToString(), other.ToString()); }
}
public class FooGroup : IEquatable<FooGroup>
{
public int GroupIndex {get; set;}
public HashSet<Foo> FooCollection {get; set;}
// -----------------------------
// Does HashSet.CreateSetComparer somehow eliminate or simplify the following code?
// -----------------------------
public override int GetHashCode()
{
int hash = GroupIndex;
foreach(Foo f in FooCollection)
hash = hash ^ (f.GetHashCode() & 0x7FFFFFFF);
return hash;
}
public bool Equals(FooGroup other)
{
// ignore missing null checks for this example
return this.GroupIndex == other.GroupIndex && this.FooCollection.SetEquals(other.FooCollection);
}
}
public class GroupCollection : IEquatable<GroupCollection>
{
public string CollectionLabel {get; set;}
public HashSet<FooGroup> AllGroups {get; set;}
// -----------------------------
// Does HashSet.CreateSetComparer somehow eliminate or simplify the following code?
// -----------------------------
public override int GetHashCode()
{
int hash = CollectionLabel.GetHashCode();
foreach(FooGroup g in AllGroups)
hash = hash ^ (g.GetHashCode() & 0x7FFFFFFF);
return hash;
}
public bool Equals(GroupCollection other)
{
// ignore missing null checks for this example
return String.Equals(this.CollectionLabel, other.CollectionLabel) && this.AllGroups.SetEquals(other.AllGroups);
}
}
Run Code Online (Sandbox Code Playgroud)
忽略有关系统设计等的争论,一个简化的用例是:想象我已经提取了一组复杂的数据,如下所示:
var newSetA = new GroupCollection{ ... }
var oldSetA = new GroupCollection{ ... }
Run Code Online (Sandbox Code Playgroud)
我只是想检查一下:
if (newSetA.Equals(oldSetA))
Process(newSetA);
Run Code Online (Sandbox Code Playgroud)
让我们从“CreateSetComparer”什么时候有用的问题开始?在这里你已经有了一个很好的想法:
特别是,如果我要比较的集合中的每个项目也包含 HashSet ,那么 CreateSetComparer 的正确用法是什么?
例如,下一个示例演示了HashSet使用默认比较器时的默认行为(仅通过引用进行比较):
var set1 = new HashSet<HashSet<int>>{
new HashSet<int>{2,3,4},
new HashSet<int>{7,8,9}
};
var set2 = new HashSet<HashSet<int>>{
new HashSet<int>{2,3,4},
new HashSet<int>{7,8,9},
};
set1.SetEquals(set2).Dump(); // false :-(
set1.SequenceEqual(set2).Dump(); // false
set1.SequenceEqual(set2, HashSet<int>.CreateSetComparer()).Dump(); // true
Run Code Online (Sandbox Code Playgroud)
也可以CreateSetComparer与 一起使用SetEquals,如下所示:
// the order of elements in the set has been change.
var set1 = new HashSet<HashSet<int>>(HashSet<int>.CreateSetComparer()){
new HashSet<int>{2,3,4},
new HashSet<int>{7,8,9}
};
var set2 = new HashSet<HashSet<int>>{
new HashSet<int>{7,8,9},
new HashSet<int>{2,3,4},
};
set1.SetEquals(set2).Dump(); // true :-)
set1.SequenceEqual(set2).Dump(); // false
set1.SequenceEqual(set2, HashSet<int>.CreateSetComparer()).Dump(); // false
Run Code Online (Sandbox Code Playgroud)
这是通常的用法,但是您可以利用它CreateSetComparer,GetHashCode尽管这不一定是您已经做的更短/更干净的操作。
// -----------------------------
// Does HashSet.CreateSetComparer somehow eliminate or simplify the following code?
// -----------------------------
private IEqualityComparer<HashSet<FooGroup>> _ecomparer =
HashSet<FooGroup>.CreateSetComparer();
public override int GetHashCode()
{
int hash = CollectionLabel.GetHashCode();
hash ^= _ecomparer.GetHashCode(AllGroups);
return hash;
}
Run Code Online (Sandbox Code Playgroud)