C# 哈希集

WBOY
发布: 2024-09-03 15:18:47
原创
201 人浏览过

唯一且不按顺序排列的元素集合称为 HashSet;在 C# 中,它位于名称空间 Systems.Collections.Generic 下,每当我们不需要集合中的任何重复项时就使用它,即,它避免将重复项插入到集合中,并且为了比较 HashSet 的性能,相比之下,HashSet 提供了更好的性能HashSet 提供的列表和设置操作具有很高的性能,并且对象所持有的元素数量就是 HashSet 的容量,随着元素数量的增加而增加。

C# 中 HashSet 的语法

HashSet<Type_of_hashset> Hashset_name = new HashSet<Type_of_hashset>();
登录后复制

以上语法表示 C# 中的 HashSet。哈希集的类型也可以用大写字母T来表示

C# 中哈希集的工作

C#中的哈希集是一个唯一的元素集合,没有任何顺序。它位于命名空间 Systems.Collections.Generic 下,每当我们不需要集合中的任何重复项时就可以使用它,即,它避免将重复项插入到集合中并比较 HashSet 的性能。与列表集合操作相比,HashSet 提供了更好的性能,而 HashSet 提供了高性能。为了理解哈希集的工作原理,我们首先用 C# 创建一个简单的哈希集程序。

代码:

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 将添加到创建的字符串类型哈希集中。上述程序的输出如下图所示:

C# 哈希集

正如已经解释过的,哈希集不允许在集合中添加重复的元素。让我们尝试将重复元素添加到上面创建的字符串类型哈希集中并了解程序的输出。然后,我们可以使用 add 方法将元素添加到集合中。

代码:

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 添加到创建的哈希集中,则不会显示错误,但它不会添加重复的字符串,并且在输出中可以看到相同的情况。上述程序的输出如下图所示:

C# 哈希集

C# 中有一种称为 Union 的方法,它存在于哈希集中。它用于将两个集合中存在的元素组合成一个调用它的集合。考虑下面的程序。

代码:

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();
}
}
}
登录后复制

在上面的程序中,创建了两个字符串类型的简单哈希集。首先,将字符串 Shobha、Shivakumar 和 Shardha 添加到字符串类型的第一个哈希集中。然后,将字符串 Shobha、Shivakumar、Shardha、Ravi 和 Nagu 添加到字符串类型的第二个哈希集中。现在我们使用 union 和 hashset 方法来组合第一个哈希集和第二个哈希集的元素。上述程序的输出如下图所示:

C# 哈希集

有一个名为 Intersect 的方法,它与 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"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist.IntersectWith(nameslist1);
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}
登录后复制

在上面的程序中,创建了两个字符串类型的简单哈希集。首先,将字符串 Shobha、Shivakumar 和 Shardha 添加到字符串类型的第一个哈希集中。接下来,将字符串 Shobha、Shivakumar、Shardha、Ravi 和 Nagu 添加到字符串类型的第二个哈希集中。现在我们使用与hashset相交的方法来组合第一个hashset和第二个hashset中的公共元素。上述程序的输出如下图所示:

C# 哈希集

C# 中的 hashset 中有一个名为 except 的方法。它用于删除所有两个集合中存在的所有元素(调用它的集合除外)。考虑下面的程序。

代码:

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();
}
}
}
登录后复制

在上面的程序中,创建了两个字符串类型的简单哈希集。首先,将字符串 Shobha、Shivakumar 和 Shardha 添加到字符串类型的第一个哈希集中。接下来,将字符串 Shobha、Shivakumar、Shardha、Ravi 和 Nagu 添加到字符串类型的第二个哈希集中。现在我们使用 except 和 hashset 中的方法来删除除调用它的集合之外的所有集合中存在的所有元素。  上述程序的输出如下图所示:

C# 哈希集

Examples of C# HashSet

Given below are the examples of C# HashSet:

Example #1

C# program to illustrate the creation of a hashset and adding elements to the hashset.

Code:

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);
}
}
}
登录后复制

Output:

C# 哈希集

Example #2

C# program to illustrate contains method in a hashset.

Code:

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");
}
}
登录后复制

Output:

C# 哈希集

以上是C# 哈希集的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!