C# 中的哈希集给出了奇怪的行为
我在 C# 中遇到 HashSet 问题...
这是我的代码:
List<int> elements = new List<int>();
for(int i = 0;i< 100000;i++)
{
elements.Add(i);
}
HashSet<int> c = new HashSet<int>();
foreach(var ele in elements)
{
c.Add(ele);
}
Console.WriteLine("Working HashSet " + c.Count);
var Numbers = new HashSet<int>();
var mycount = 0;
using (TextReader reader = File.OpenText(@"myfile.txt"))
{
while ((line = reader.ReadLine()) != null)
{
mycount++;
int parsed = int.Parse(line);
Numbers.Add(parsed);
}
}
Console.WriteLine("my counter took" + mycount);
Console.WriteLine("Bad HashSet" + Numbers.Count);
Working HashSet 100 000
我的计数器花了 500 000
Bad HashSet 9999
为什么第二个 hashset 没有添加 500 000 个项目??? 这对我来说是个谜
I have a problem with HashSet in C#....
this is my code:
List<int> elements = new List<int>();
for(int i = 0;i< 100000;i++)
{
elements.Add(i);
}
HashSet<int> c = new HashSet<int>();
foreach(var ele in elements)
{
c.Add(ele);
}
Console.WriteLine("Working HashSet " + c.Count);
var Numbers = new HashSet<int>();
var mycount = 0;
using (TextReader reader = File.OpenText(@"myfile.txt"))
{
while ((line = reader.ReadLine()) != null)
{
mycount++;
int parsed = int.Parse(line);
Numbers.Add(parsed);
}
}
Console.WriteLine("my counter took" + mycount);
Console.WriteLine("Bad HashSet" + Numbers.Count);
Working HashSet 100 000
my counter took 500 000
Bad HashSet 9999
why the second hashset is not adding 500 000 items????
this is a mistery to me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HashSet
不会添加重复的数字,因为这就是集合的工作方式。例如,假设这些是
myfile.txt
的前几行:您将迭代 7 个值,但这些行中只有 4 个唯一数字,并且 HashSet 不会添加 1 的重复项、2 或 3。在您的例子中,您有 500,000 行,但只有 9,999 个唯一数字。
A
HashSet
will not add duplicate numbers, because that's the way sets work.For example, let's say these are the first few lines of
myfile.txt
:You will be iterating over 7 values, but there are only 4 unique numbers in those lines, and HashSet will not add duplicates of 1, 2, or 3. In your case, you have 500,000 lines but only 9,999 unique numbers.
您的列表包含 500.000 个项目,其中 9999 个是唯一的。
Your list contains 500.000 items of which there are 9999 unique.
大概有一些重复的。
HashSet
表示集合,并提供集合操作,因此得名。这就是我们使用它的目的。Presumably there're some duplicates.
HashSet<T>
represents a set, and provides set operations, hence the name. This is what we use it for.