C# 哈希集

WBOY
發布: 2024-09-03 15:18:47
原創
202 人瀏覽過

唯一且不按順序排列的元素集合稱為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學習者快速成長!