Để cho phép chính xác hơn, hãy chứng minh bằng các ví dụ,
Bạn không thể sử dụng HashSet như trong ví dụ sau.
HashSet<string> hashSet1 = new HashSet<string>(){"1","2","3"};
for (int i = 0; i < hashSet1.Count; i++)
Console.WriteLine(hashSet1[i]);
hashSet1[i]
sẽ tạo ra một lỗi:
Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.HashSet'
Bạn có thể dùng lệnh foreach:
foreach (var item in hashSet1)
Console.WriteLine(item);
Bạn không thể thêm các mục trùng lặp để HashSet khi Danh sách cho phép bạn làm điều này và trong khi bạn đang thêm một mục vào HashSet, bạn có thể kiểm tra xem nó có chứa s mục hay không.
HashSet<string> hashSet1 = new HashSet<string>(){"1","2","3"};
if (hashSet1.Add("1"))
Console.WriteLine("'1' is successfully added to hashSet1!");
else
Console.WriteLine("'1' could not be added to hashSet1, because it contains '1'");
HashSet có một số chức năng hữu ích như IntersectWith
, UnionWith
, IsProperSubsetOf
, ExceptWith
, SymmetricExceptWith
, vv
IsProperSubsetOf
:
HashSet<string> hashSet1 = new HashSet<string>() { "1", "2", "3", "4" };
HashSet<string> hashSet2 = new HashSet<string>() { "2", "4", "6", "8" };
HashSet<string> hashSet3 = new HashSet<string>() { "1", "2", "3", "4", "5" };
if (hashSet1.IsProperSubsetOf(hashSet3))
Console.WriteLine("hashSet3 contains all elements of hashSet1.");
if (!hashSet1.IsProperSubsetOf(hashSet2))
Console.WriteLine("hashSet2 does not contains all elements of hashSet1.");
UnionWith
:
HashSet<string> hashSet1 = new HashSet<string>() { "3", "4" };
HashSet<string> hashSet2 = new HashSet<string>() { "2", "4", "6", "8" };
hashSet1.UnionWith(hashSet2); //hashSet1 -> 3, 2, 4, 6, 8
IntersectWith
:
HashSet<string> hashSet1 = new HashSet<string>() { "3", "4", "8" };
HashSet<string> hashSet2 = new HashSet<string>() { "2", "4", "6", "8" }
hashSet1.IntersectWith(hashSet2);//hashSet1 -> 4, 8
ExceptWith
:
HashSet<string> hashSet1 = new HashSet<string>() { "1", "2", "3", "5", "6" };
HashSet<string> hashSet2 = new HashSet<string>() { "1", "2", "3", "4" };
hashSet1.ExceptWith(hashSet2);//hashSet1 -> 5, 6
SymmetricExceptWith
:
HashSet<string> hashSet1 = new HashSet<string>() { "1", "2", "3", "5", "6" };
HashSet<string> hashSet2 = new HashSet<string>() { "1", "2", "3", "4" };
hashSet1.SymmetricExceptWith(hashSet2);//hashSet1 -> 4, 5, 6
Bằng cách này, thứ tự không được bảo quản trong HashSets. Trong ví dụ này, chúng tôi đã thêm yếu tố "2" cuối cùng nhưng nó là theo thứ tự thứ hai:
HashSet<string> hashSet1 = new HashSet<string>() { "3", "4", "8" };
hashSet1.Add("1"); // 3, 4, 8, 1
hashSet1.Remove("4"); // 3, 8, 1
hashSet1.Add("2"); // 3, 2 ,8, 1
Một số đọc về chủ đề: [C# /. NET nguyên tắc cơ bản: Chọn lớp sưu tập phù hợp] (http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the -right-collection-class.aspx) –
Tôi khuyên bạn nên tham khảo các bài viết trên Wikipedia về http://en.wikipedia.org/wiki/Hash_table và http://en.wikipedia.org/wiki/Dynamic_array. – mquander
Để biết hiệu suất liên quan, xem [hashset-vs-list-performance] (http: // stackoverflow.com/questions/150750/hashset-vs-list-performance) – nawfal