C#程式合併兩個雜湊表集合


C# 中的雜湊表集合儲存鍵值對。此集合中的每個元素或項都是一個鍵值對,即此集合是一個雙元素集合。鍵是唯一的、非空的,用於訪問雜湊表中的元素。

雜湊表集合是不可變的,不能包含重複的元素。這意味著鍵值組合必須唯一。但是,值可以為空或重複。.Net Framework 提供了一個 HashTable 類來實現雜湊表集合,幷包含實現雜湊表所需的功能,無需任何額外的編碼。

雜湊表集合中的每個元素都是一個 DictionaryEntry 物件,具有兩個屬性:一個鍵元素和一個值元素。當向雜湊表新增元素時,會自動生成雜湊碼。此雜湊碼是內部的,並且是隱藏的。雜湊表集合中的元素按隱藏的雜湊碼排序。因此,雜湊表元素被認為是隨機選擇的。

透過對雜湊表集合的簡要介紹,讓我們看看如何合併兩個雜湊表集合。

如何合併兩個雜湊表集合?

HashTable 類由 System.Collections 名稱空間提供。該名稱空間僅包含可用於構造雜湊表物件並執行新增/刪除元素、計算元素數量等操作的基本類庫。沒有提供可用於將兩個雜湊表合併在一起的方法/函式。

我們必須設計我們自己的方法來合併兩個雜湊表。我們知道雜湊表的容量或大小是雜湊表包含的元素數量。當元素插入雜湊表時,雜湊表的大小會透過重新分配自動增長。

因此,當我們將兩個雜湊表合併在一起時,我們將把一個雜湊表的元素新增到另一個雜湊表中。當我們新增元素時,此雜湊表的大小將相應調整。

方法

  • 建立兩個雜湊表物件。

  • 使用 Add 方法將元素填充到兩個表中。

  • 使用鍵遍歷第二個雜湊表,並將每個鍵值對新增到第一個雜湊表中(如果第一個雜湊表中不存在當前項(正在遍歷的鍵))。

  • 列印結果雜湊表。

注意:在新增鍵之前,我們顯式地檢查雜湊表中是否存在該鍵,因為雜湊表不允許新增重複的鍵。

示例

以上方法轉換為如下所示的 C# 程式。

using System;
using System. Collections;
class MyHashTable {
   static public void Main() {
      Hashtable indianNumberSystem = new Hashtable();
      indianNumberSystem.Add(1,"Ones");
      indianNumberSystem.Add(10,"Tens");
      indianNumberSystem.Add(100,"Hundred");
      indianNumberSystem.Add(1000,"Thousand");
      Console.WriteLine("Contents of indianNumberSystem hashtable:");
         foreach(DictionaryEntry ele1 in indianNumberSystem){
         Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
      }
      Hashtable langCodes = new Hashtable();
      langCodes.Add("C++","CPlusPlus");
      langCodes.Add("C#","CSharp");
      langCodes.Add("Java","Java");
      langCodes.Add("PL","Perl");
      Console.WriteLine("
Contents of langCodes Hashtable:"); foreach(DictionaryEntry ele1 in indianNumberSystem){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } foreach (DictionaryEntry entry in langCodes) { if(!indianNumberSystem.ContainsKey(entry.Key)) { indianNumberSystem.Add(entry.Key, entry.Value); }} Console.WriteLine("
Key, Value pairs after merging langCodes to indianNumberSystem:"); foreach(DictionaryEntry ele1 in indianNumberSystem){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } } }

這裡我們有兩個雜湊表,分別是 indianNumberSystem 和 langCodes。

雜湊表 indianNumberSystem 包含以下資料:

1

"Ones"

10

"Tens"

100

"Hundred"

1000

"Thousand"

雜湊表 langCodes 包含以下資料。

C++

"CPlusPlus"

C#

“CSharp"

Java

"Java"

PL

"Perl"

我們首先顯示這兩個表的內容。然後,我們使用其鍵遍歷 langCodes 雜湊表。在遍歷迴圈內,我們首先檢查雜湊表 indianNumberSystem 是否具有相同的鍵。如果鍵不存在,我們將當前鍵指向的 langCodes 元素新增到 indianNumberSystem 雜湊表中。

輸出

最後,我們顯示合併後的表。

Contents of indianNumberSystem hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
Contents of langCodes Hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
Key, Value pairs after merging langCodes to indianNumberSystem:
100 (Hundred)
1000 (Thousand)
PL (Perl)
10 (Tens)
C# (CSharp)
Java (Java)
C++ (CPlusPlus)
1 (Ones)

從生成的輸出中我們可以看到兩個表都正確合併了。

示例

現在讓我們考慮另一個示例,下面給出了 C# 程式。

using System;
using System. Collections;
using System.Collections.Generic;
class MyHashTable {
   static public void Main() {
      Hashtable indianNumberSystem = new Hashtable();
      indianNumberSystem.Add(1,"Ones");
      indianNumberSystem.Add(10,"Tens");
      indianNumberSystem.Add(100,"Hundred");
      indianNumberSystem.Add(1000,"Thousand");
      Console.WriteLine("Contents of indianNumberSystem hashtable:");
         foreach(DictionaryEntry ele1 in indianNumberSystem){
            Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
      }
      Hashtable NumberNames = new Hashtable();
      NumberNames.Add(1,"One");
      NumberNames.Add(2,"Two");
      NumberNames.Add(3,"Three");
      NumberNames.Add(4,"Four");
      Console.WriteLine("
Contents of NumberNames Hashtable:"); foreach(DictionaryEntry ele1 in NumberNames){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } foreach (DictionaryEntry entry in NumberNames) { if(!indianNumberSystem.ContainsKey(entry.Key)) { indianNumberSystem.Add(entry.Key, entry.Value); }} Console.WriteLine("
Key, Value pairs after merging NumberNames to indianNumberSystem:"); foreach(DictionaryEntry ele1 in indianNumberSystem){ Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value); } } }

該程式與上一個程式相同,只是我們將 langCodes 雜湊表替換為 NumberNames 雜湊表。NumberNames 雜湊表包含以下元素。

1

"One"

2

“Two"

3

"Three

4

"Four"

輸出

如我們所見,雜湊表 indianNumberSystem 和 NumberNames 有共同的資料。現在讓我們執行此程式以檢查合併是如何發生的。

Contents of indianNumberSystem hashtable:
1000 (Thousand)
10 (Tens)
100 (Hundred)
1 (Ones)
Contents of NumberNames Hashtable:
4 (Four)
3 (Three)
2 (Two)
1 (One)
Key, Value pairs after merging NumberNames to indianNumberSystem:
100 (Hundred)
1000 (Thousand)
10 (Tens)
4 (Four)
3 (Three)
2 (Two)
1 (Ones)

從上面的輸出可以看出,我們可以看到 NumberNames 中的資料元素(鍵=1)沒有新增到 indianNumberSystem 雜湊表中。這是因為不允許重複。

結論

因此,我們可以透過將一個雜湊表的資料複製或新增到另一個雜湊表來合併兩個雜湊表集合。每當兩個雜湊表中存在公共鍵時,都不會新增重複的鍵。但是程式設計師必須確保在新增一個雜湊表的資料時進行檢查,以避免意外新增資料,因為這會導致不可預測的結果。

更新於: 2022年12月22日

497 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告