HybridDictionary cho phép chỉ định dung lượng ban đầu (initial capacity) qua constructor. Điều này giúp tối ưu hiệu năng bằng cách tránh việc resize nội bộ khi thêm phần tử. HybridDictionary tự động chuyển từ ListDictionary (khi nhỏ) sang Hashtable (khi lớn).
Ví dụ 1: Tạo với kích thước ban đầu 5
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
// Tạo HybridDictionary với initial capacity = 5
HybridDictionary dict = new HybridDictionary(5);
dict.Add("A", "AB");
dict.Add("B", "BC");
dict.Add("C", "DE");
dict.Add("D", "FG");
dict.Add("E", "HI");
Console.WriteLine("Các cặp Key/Value...");
foreach (DictionaryEntry d in dict)
{
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}
Kết quả
Các cặp Key/Value... Key = A, Value = AB Key = B, Value = BC Key = C, Value = DE Key = D, Value = FG Key = E, Value = HI
Ví dụ 2: Kích thước ban đầu 10
using System;
using System.Collections;
using System.Collections.Specialized;
public class Demo
{
public static void Main()
{
HybridDictionary dict = new HybridDictionary(10);
dict.Add("1", 100);
dict.Add("2", 200);
dict.Add("3", 300);
dict.Add("4", 400);
dict.Add("5", 500);
dict.Add("6", 600);
dict.Add("7", 700);
dict.Add("8", 800);
dict.Add("9", 900);
dict.Add("10", 1000);
Console.WriteLine("Các cặp Key/Value...");
foreach (DictionaryEntry d in dict)
{
Console.WriteLine("Key = " + d.Key + ", Value = " + d.Value);
}
}
}
Kết quả (thứ tự không đảm bảo do Hashtable)
Các cặp Key/Value... Key = 10, Value = 1000 Key = 1, Value = 100 Key = 2, Value = 200 Key = 3, Value = 300 Key = 4, Value = 400 Key = 5, Value = 500 Key = 6, Value = 600 Key = 7, Value = 700 Key = 8, Value = 800 Key = 9, Value = 900
Các Constructor của HybridDictionary
| Constructor | Mô tả |
|---|---|
HybridDictionary() |
Mặc định, capacity = 0 |
HybridDictionary(int initialSize) |
Chỉ định dung lượng ban đầu |
HybridDictionary(int initialSize, bool caseInsensitive) |
Chỉ định dung lượng và case-sensitive |
HybridDictionary(IDictionary) |
Sao chép từ dictionary khác |
HybridDictionary(IEqualityComparer) |
Custom comparer |
Cơ chế chuyển đổi nội bộ
- ≤ 9 phần tử: Sử dụng
ListDictionary(linked list) - O(n) lookup - > 9 phần tử: Chuyển sang
Hashtable- O(1) lookup - Ngưỡng chuyển đổi có thể thay đổi tùy phiên bản .NET
Lưu ý quan trọng
⚠️ Đã lỗi thời:
HybridDictionary thuộc namespace System.Collections.Specialized và không phải generic. Microsoft khuyến nghị sử dụng Dictionary<TKey, TValue> cho code mới.
So sánh với Dictionary<TKey, TValue>
// Dictionary hiện đại - type-safe, hiệu năng tốt
Dictionary<string, string> dict = new Dictionary<string, string>(5); // Capacity = 5
dict.Add("A", "AB");
// ...
// Dictionary cũng có constructor capacity
var dict2 = new Dictionary<int, string>(100); // Pre-allocate cho 100 phần tử
Khi nào nên dùng initial capacity?
- Biết trước số lượng phần tử ước lượng
- Tránh overhead của resize (allocate mảng mới, copy dữ liệu)
- Hiệu năng tốt hơn khi thêm nhiều phần tử liên tiếp
- Với Dictionary: capacity là gợi ý, không phải giới hạn cứng
HybridDictionary vs Dictionary
| Đặc điểm | HybridDictionary | Dictionary<TKey,TValue> |
|---|---|---|
| Type-safe | Không | Có |
| Generic | Không | Có |
| Hiệu năng nhỏ | Tốt (ListDictionary) | Tốt |
| Hiệu năng lớn | Tốt (Hashtable) | Rất tốt |
| Trạng thái | Legacy | Khuyến nghị |