我的程序中有两个列表——一个主列表和另一个不断更新的临时列表。每隔一段时间,临时列表就会刷新到主列表中。
主列表是 HashSet(用于无重复),临时列表是 List(用于索引功能)。我通过调用将后者冲入前者
HashSet<T>.UnionWith(List<T>)
在我的测试中,我发现重复项进入列表,但我认为这在 HashSet 中是不可能的。有人可以确认/更正吗?我一直无法在 MSDN 中找到它。
我的程序中有两个列表——一个主列表和另一个不断更新的临时列表。每隔一段时间,临时列表就会刷新到主列表中。
主列表是 HashSet(用于无重复),临时列表是 List(用于索引功能)。我通过调用将后者冲入前者
HashSet<T>.UnionWith(List<T>)
在我的测试中,我发现重复项进入列表,但我认为这在 HashSet 中是不可能的。有人可以确认/更正吗?我一直无法在 MSDN 中找到它。
如果您的类型覆盖并且正确,这是不可能的。我的猜测是您的类型没有正确执行此操作。(或者您的哈希集是使用自定义相等比较器创建的,它不符合您的要求。)GetHashCode()
Equals()
如果您认为不是这种情况,请发布代码:)
但是,是的,它确实会在正常使用时防止重复。
列表(用于索引功能)。
您需要一个用于索引的字典。尽管如此,这里有一个非常简单的程序来说明您的问题:
class Program
{
static void Main(string[] args)
{
int totalCats = 0;
HashSet<Cat> allCats = new HashSet<Cat>();
List<Cat> tempCats = new List<Cat>();
//put 10 cats in
for (int i = 0; i < 10; i++)
{
tempCats.Add(new Cat(i));
totalCats += 1;
}
//add the cats to the final hashset & empty the temp list
allCats.UnionWith(tempCats);
tempCats = new List<Cat>();
//create 10 identical cats
for (int i = 0; i < 10; i++)
{
tempCats.Add(new Cat(i));
totalCats += 1;
}
//join them again
allCats.UnionWith(tempCats);
//print the result
Console.WriteLine("Total cats: " + totalCats);
foreach (Cat curCat in allCats)
{
Console.WriteLine(curCat.CatNumber);
}
}
}
public class Cat
{
public int CatNumber { get; set; }
public Cat(int catNum)
{
CatNumber = catNum;
}
}
您的问题是您没有覆盖 GetHashCode() 和 Equals()。您需要同时拥有这两者才能使哈希集保持唯一。
这将起作用,但是 GetHashCode() 函数应该更加健壮。我建议阅读 .NET 是如何做到的:
class Program
{
static void Main(string[] args)
{
int totalCats = 0;
HashSet<Cat> allCats = new HashSet<Cat>();
List<Cat> tempCats = new List<Cat>();
//put 10 cats in
for (int i = 0; i < 10; i++)
{
tempCats.Add(new Cat(i));
totalCats += 1;
}
//add the cats to the final hashset & empty the temp list
allCats.UnionWith(tempCats);
tempCats = new List<Cat>();
//create 10 identical cats
for (int i = 0; i < 10; i++)
{
tempCats.Add(new Cat(i));
totalCats += 1;
}
//join them again
allCats.UnionWith(tempCats);
//print the result
Console.WriteLine("Total cats: " + totalCats);
foreach (Cat curCat in allCats)
{
Console.WriteLine(curCat.CatNumber);
}
Console.ReadKey();
}
}
public class Cat
{
public int CatNumber { get; set; }
public Cat(int catNum)
{
CatNumber = catNum;
}
public override int GetHashCode()
{
return CatNumber;
}
public override bool Equals(object obj)
{
if (obj is Cat)
{
return ((Cat)obj).CatNumber == CatNumber;
}
return false;
}
}