The Hashtable
is a non-generic collection that stores key-value pairs, similar to generic Dictionary collection. It optimizes lookups by computing the hash code of each key and stores it in a different bucket internally and then matches the hash code of the specified key at the time of accessing values.
Đặc điểm Hashtable
Hashtable
lưu trữ các cặp khóa-giá trị- Thuộc không gian tên
System.Collections
- Triển khai giao diện IDictionary
- Các khóa phải là duy nhất và không thể rỗng
- Các giá trị có thể là null hoặc trùng lặp
- Các giá trị có thể được truy cập bằng cách chuyển khóa liên quan trong bộ chỉ mục e. g.
myHashtable[key]
- Các phần tử được lưu trữ dưới dạng đối tượng DictionaryEntry
Tạo một Hashtable
Ví dụ sau minh họa việc tạo Hashtable và thêm các phần tử
Ví dụ. Tạo và thêm phần tử
Hashtable numberNames = new Hashtable[];
numberNames.Add[1,"One"]; //adding a key/value using the Add[] method
numberNames.Add[2,"Two"];
numberNames.Add[3,"Three"];
//The following throws run-time exception: key already added.
//numberNames.Add[3, "Three"];
foreach[DictionaryEntry de in numberNames]
Console.WriteLine["Key: {0}, Value: {1}", de.Key, de.Value];
//creating a Hashtable using collection-initializer syntax
var cities = new Hashtable[]{
{"UK", "London, Manchester, Birmingham"},
{"USA", "Chicago, New York, Washington"},
{"India", "Mumbai, New Delhi, Pune"}
};
foreach[DictionaryEntry de in cities]
Console.WriteLine["Key: {0}, Value: {1}", de.Key, de.Value];
Thử nó
Bộ sưu tập Hashtable
có thể bao gồm tất cả các thành phần của Từ điển, như hình bên dưới
Ví dụ. Thêm từ điển vào Hashtable
Dictionary dict = new Dictionary[];
dict.Add[1, "one"];
dict.Add[2, "two"];
dict.Add[3, "three"];
Hashtable ht = new Hashtable[dict];
Thử nó
Cập nhật Hashtable
Bạn có thể truy xuất giá trị của khóa hiện có từ Hashtable
bằng cách chuyển khóa vào bộ chỉ mục. Hashtable
là một bộ sưu tập không chung chung, vì vậy bạn phải nhập các giá trị truyền trong khi truy xuất nó
Ví dụ. Cập nhật Hashtable
//creating a Hashtable using collection-initializer syntax
var cities = new Hashtable[]{
{"UK", "London, Manchester, Birmingham"},
{"USA", "Chicago, New York, Washington"},
{"India", "Mumbai, New Delhi, Pune"}
};
string citiesOfUK = [string] cities["UK"]; //cast to string
string citiesOfUSA = [string] cities["USA"]; //cast to string
Console.WriteLine[citiesOfUK];
Console.WriteLine[citiesOfUSA];
cities["UK"] = "Liverpool, Bristol"; // update value of UK key
cities["USA"] = "Los Angeles, Boston"; // update value of USA key
if[!cities.ContainsKey["France"]]{
cities["France"] = "Paris";
}
Thử nó
Xóa phần tử trong Hashtable
Phương thức
Dictionary dict = new Dictionary[];
dict.Add[1, "one"];
dict.Add[2, "two"];
dict.Add[3, "three"];
Hashtable ht = new Hashtable[dict];
1 xóa khóa-giá trị khớp với giá trị được chỉ định trong Hashtable
. Nó ném Dictionary dict = new Dictionary[];
dict.Add[1, "one"];
dict.Add[2, "two"];
dict.Add[3, "three"];
Hashtable ht = new Hashtable[dict];
3 nếu không tìm thấy khóa được chỉ định trong Hashtable, vì vậy hãy kiểm tra khóa hiện có bằng phương pháp Dictionary dict = new Dictionary[];
dict.Add[1, "one"];
dict.Add[2, "two"];
dict.Add[3, "three"];
Hashtable ht = new Hashtable[dict];
4 trước khi xóa