如何在 C# 中建立雜湊表集合?
雜湊表是 C# 中的非泛型集合。它儲存鍵值對,類似於泛型“字典”集合。雜湊表定義在
System. Collections. namespace.
雜湊表計算每個鍵的雜湊碼,並將其儲存在不同的桶中。當訪問值時,雜湊碼與指定鍵的雜湊碼進行匹配。因此,雜湊表優化了查詢。
在本教程中,我們將瞭解如何在 C# 中建立雜湊表集合。
雜湊表特性
在開始建立雜湊表之前,讓我們瞭解一下 C# 中雜湊表集合的一些主要特性。
雜湊表集合儲存鍵值對。
雜湊表是 C# 中 System.Collections 名稱空間的一部分,並實現 IDictionary 介面。雜湊表的元素儲存為 DictionaryEntry 物件。
雜湊表的鍵不能為 null,並且必須唯一。但是,值可以為 null 或重複。
可以使用索引器中的鍵訪問雜湊表中的值,就像訪問陣列值一樣。
雜湊表中的鍵是不可變物件。每個鍵物件都提供一個雜湊函式。
典型的雜湊表類實現 C# 的 IDictionary、ICollection、ISerializable、IEnumerable、IDeserializationCallback 和 ICloneable 介面。
儲存在雜湊表中的元素可以是相同型別或不同型別的。
記住這些主要特性,讓我們現在討論如何在 C# 中建立雜湊表。
如何在 C# 中建立雜湊表集合?
C# 的雜湊表類提供 16 個過載的建構函式來建立雜湊表。
下表顯示了我們將在本文中使用的雜湊表建構函式。
| 建構函式 | 描述 |
|---|---|
| Hashtable() | 初始化新的、空的雜湊表類例項,具有預設的初始容量、雜湊碼提供程式、比較器和負載因子。 |
| Hashtable(IDictionary) | 建立一個新的雜湊表類例項,並使用指定字典的內容對其進行初始化。 |
注意 - 要了解有關 C# 中雜湊表類的更多資訊,請閱讀我們的文章 C# - 雜湊表類。
讓我們看看通常在 C# 中建立雜湊表集合的步驟。
首先,我們在程式中包含 System.Collections 名稱空間
using System. Collections;
接下來,我們使用 Hashtable 類建立一個雜湊表。為此,我們使用預設建構函式。
Hashtable hashtable_name = new Hashtable();
現在我們可以使用“Add()”方法向雜湊表新增元素。
因此,我們可以在建立雜湊表例項時初始化整個雜湊表,也可以使用 Add() 方法逐個向雜湊表新增元素。
示例 1
下面的程式演示了在 C# 中建立雜湊表。
using System;
using System. Collections;
class MyHashTable {
// Main Method
static public void Main() {
// Create hashtable using the default constructor
Hashtable indianNumberSystem = new Hashtable();
//add a key/value pair using the Add() method
indianNumberSystem.Add(1,"Ones");
indianNumberSystem.Add(10,"Tens");
indianNumberSystem.Add(100,"Hundred");
indianNumberSystem.Add(1000,"Thousand");
indianNumberSystem.Add(10000,"Ten Thousand");
indianNumberSystem.Add(100000,"Lac");
indianNumberSystem.Add(1000000,"Ten Lac");
indianNumberSystem.Add(10000000,"Crore");
//display HashTable contents
Console.WriteLine("Key, Value pairs from Indian Number System:");
foreach(DictionaryEntry ele1 in indianNumberSystem){
Console.WriteLine("{0} ({1}) ", ele1.Key, ele1.Value);
}
}
}
在上面的程式中,我們使用預設建構函式定義了一個雜湊表例項。然後,我們使用Add()方法向雜湊表新增鍵值對。最後,使用 for-each 迴圈逐個列印雜湊表內容。
輸出
上面的程式生成以下輸出。
Key, Value pairs from Indian Number System: 100 (Hundred) 1000 (Thousand) 10 (Tens) 1000000 (Ten Lac) 100000 (Lac) 10000000 (Crore) 10000 (Ten Thousand) 1 (Ones)
該程式顯示了一個包含印度數字系統位值的雜湊表。請注意,由於這是一個簡單的建立雜湊表並向其中新增內容的程式,因此輸出未格式化。
示例 2
讓我們來看另一個在 C# 中建立雜湊表的例子。下面的程式使用不同的建構函式來建立雜湊表。
using System;
using System.Collections;
class MyHashTable {
// Main Method
static public void Main() {
// Create hashtable without using Add method
Hashtable my_hashtable1 = new Hashtable() {{"K1", "New York"}};
// Adding key/value pair in the hashtable using Add() method
my_hashtable1.Add("K2", "Paris");
my_hashtable1.Add("K3", "London");
my_hashtable1.Add("K4", "Mumbai");
my_hashtable1.Add("K5", "Berlin");
Console.WriteLine("Key, Value pairs from my_hashtable1:");
foreach(DictionaryEntry ele1 in my_hashtable1){
Console.WriteLine("{0} and {1} ", ele1.Key, ele1.Value);
}
}
}
正如我們在上面的程式碼中看到的,我們首先建立一個包含一對鍵值對的雜湊表物件。然後,我們使用雜湊表類的add()方法向雜湊表新增元素。最後,我們使用 for-each 迴圈遍歷雜湊表物件以列印每個雜湊表元素(鍵值對)。
輸出
上面的程式產生以下輸出。
Key, Value pairs from my_hashtable1: K2 and Paris K1 and New York K3 and London K4 and Mumbai K5 and Berlin
在上面的輸出中,鍵值對按值的逆字母順序顯示。這是雜湊表的預設輸出,因為我們沒有提供任何程式碼來格式化輸出。雜湊表類提供各種方法來組織/格式化輸出,我們將在後續教程中學習。
在本教程中,我們討論瞭如何在 C# 中建立雜湊表集合。雜湊表是一個非泛型的鍵值對集合。雜湊表中的鍵是唯一、非 null 值。值可以為 null 並重復。我們可以使用 System.Collections 介面提供的雜湊表類在 C# 中建立雜湊表,並使用該類提供的各種方法對其進行修改。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP