C# HashSet简介
在C#中,一个唯一的、不按顺序排列的元素集合被称为HashSet,它属于Systems.Collections.Generic命名空间,当我们在集合中不需要任何重复的元素时,就可以使用它,也就是说。在比较HashSet的性能时,HashSet提供了比List更好的性能,HashSet提供的集合操作具有很高的性能,对象持有的元素数量是HashSet的容量,随着元素数量的增加而增加。
C#中HashSet的语法
HashSet<Type_of_hashset> Hashset_name = new HashSet<Type_of_hashset>();
上面的语法代表了C#中的HashSet。哈希集的类型也可以用大写字母T来表示。
Hashset在C#中的作用
C#中的哈希集是一个没有任何顺序的元素的唯一集合。它隶属于Systems.Collections.Generic命名空间,当我们在集合中不需要任何重复的元素时,就会使用它,即避免在集合中插入重复的元素,并比较HashSet的性能。与列表集操作相比,HashSet提供了更好的性能,HashSet提供了高性能。为了理解hashSet的工作,让我们先用C#语言创建一个简单的hashSet程序。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
在上面的程序中,创建了一个简单的字符串类型的哈希集。字符串Shobha、Shivakumar和Shardha被添加到创建的字符串类型的hashset中。上述程序的输出显示在下面的快照中。
正如已经解释过的,hashset不允许在集合中添加重复的元素。让我们尝试在上面创建的字符串类型的hashset中添加一个重复的元素,并了解程序的输出。然后,我们可以使用add a方法将这些元素添加到集合中。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
nameslist.Add("Shobha");
//collection cannot contain duplicate elements.
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
在上面的程序中,创建了一个简单的字符串类型的哈希集。然后,字符串Shobha、Shivakumar和Shardha被添加到创建的字符串类型的哈希集中。如果我们尝试使用add方法将重复的字符串Shobha添加到创建的hashset中,没有显示错误,但它没有添加重复的字符串,在输出中也可以看到同样的情况。上述程序的输出在下面的快照中显示。
在C#的hashset中,有一个方法叫做Union with方法。它用于将两个集合中的元素合并成一个单一的集合,并对其进行调用。考虑一下下面的程序。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist.UnionWith(nameslist1);
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
在上面的程序中,创建了两个简单的字符串类型的hashset。首先,字符串Shobha, Shivakumar, 和Shardha被添加到第一个字符串类型的哈希集。然后,字符串Shobha, Shivakumar, Shardha, Ravi, 和Nagu被添加到第二个字符串类型的哈希集。现在我们使用union的方法is hashset来合并第一个hashset和第二个hashset的元素。上述程序的输出在下面的快照中显示。
在C#中,有一个叫做Intersect的方法,它与hashset中的方法是一样的。它被用来将两个集合中共同存在的元素合并成一个单一的集合,并对其进行调用。考虑一下下面的程序。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist.IntersectWith(nameslist1);
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
在上面的程序中,创建了两个简单的字符串类型的hashset。首先,字符串Shobha, Shivakumar, 和Shardha被添加到第一个字符串类型的哈希集。接下来,字符串Shobha, Shivakumar, Shardha, Ravi, 和Nagu被添加到第二个字符串类型的哈希集。现在,我们使用交叉的方法is hashset来合并第一个hashset和第二个hashset中的共同元素。上述程序的输出在下面的快照中显示。
在C#的hashset中,有一个叫做 Except with 的方法。它被用来删除所有两个集合中的元素,除了它被调用的集合。考虑一下下面的程序。
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist1.ExceptWith(nameslist);
foreach(var nam in nameslist1) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
在上面的程序中,创建了两个简单的字符串类型的hashset。首先,字符串Shobha, Shivakumar, 和Shardha被添加到第一个字符串类型的哈希集。接下来,字符串Shobha, Shivakumar, Shardha, Ravi, 和Nagu被添加到第二个字符串类型的哈希集。现在我们利用hashset中的Except方法来移除所有集合中的元素,除了调用该方法的集合。 上述程序的输出在下面的快照中显示。
C# HashSet的例子
下面给出的是C# HashSet的例子。
例子#1
用C#程序来说明创建哈希集和向哈希集添加元素的过程。
代码:
using System;
using System.Collections.Generic;
class Hashset {
// Calling the main method
public static void Main()
{
// An hashset of even numbers is created
HashSet<int> even = new HashSet<int>();
// Adding the elements in to the hashset
for (int i = 0; i < 5; i++) {
even.Add(2 * i);
}
// Printing the elements in the hashset
foreach(int j in even)
{
Console.WriteLine(j);
}
}
}
输出:
例#2
用C#程序来说明hashset中包含的方法。
代码:
using System;
using System.Collections.Generic;
class hashset {
// Calling the main method
public static void Main()
{
// An hashset consisting of strings is created
HashSet<string> cl = new HashSet<string>();
// Elements are inserted into the hashset
cl.Add("Shobha");
cl.Add("Shivakumar");
cl.Add("Shardha");
cl.Add("Ravi");
// Using the contains method to check if an element is present in the collection
if (cl.Contains("Shobha"))
Console.WriteLine("The element specified is present");
else
Console.WriteLine("The element specified is not present");
}
}
输出: