Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Lấy bộ đếm (enumerator) duyệt qua HashSet trong C#

Giới thiệu

HashSet<T> (namespace System.Collections.Generic) là tập hợp các phần tử duy nhất, không thứ tự. Để duyệt qua các phần tử, ta có thể dùng foreach hoặc lấy bộ đếm qua phương thức GetEnumerator().

Ví dụ 1: Duyệt HashSet với enumerator

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(string[] args)
    {
        HashSet set1 = new HashSet();
        set1.Add("A");
        set1.Add("B");
        set1.Add("C");
        set1.Add("D");
        set1.Add("E");
        set1.Add("F");
        set1.Add("G");
        set1.Add("H");

        Console.WriteLine("Các phần tử trong HashSet1...");
        foreach (string res in set1)
        {
            Console.WriteLine(res);
        }

        HashSet set2 = new HashSet();
        set2.Add("John");
        set2.Add("Jacob");
        set2.Add("Ryan");
        set2.Add("Tom");
        set2.Add("Andy");
        set2.Add("Tim");
        set2.Add("Steve");
        set2.Add("Mark");

        Console.WriteLine("Các phần tử trong HashSet2... (Enumerator duyệt qua HashSet)");
        HashSet.Enumerator demoEnum = set2.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            string res = demoEnum.Current;
            Console.WriteLine(res);
        }

        Console.WriteLine("HashSet1 có bằng HashSet2? = " + set1.Equals(set2));
        Console.WriteLine("Số lượng phần tử HashSet2 = " + set2.Count);
        set2.Clear();
        Console.WriteLine("Số lượng HashSet2 (sau Clear) = " + set2.Count);
    }
}

Kết quả

Các phần tử trong HashSet1...
A
B
C
D
E
F
G
H
Các phần tử trong HashSet2... (Enumerator duyệt qua HashSet)
John
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
HashSet1 có bằng HashSet2? = False
Số lượng phần tử HashSet2 = 8
Số lượng HashSet2 (sau Clear) = 0

Ví dụ 2: HashSet loại bỏ trùng lặp và kiểm tra siêu tập

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        HashSet set1 = new HashSet();
        set1.Add("AB");
        set1.Add("CD");
        set1.Add("EF");
        set1.Add("AB"); // trùng lặp, sẽ bị bỏ qua
        set1.Add("IJ");
        set1.Add("KL");
        set1.Add("EF"); // trùng lặp
        set1.Add("OP");

        Console.WriteLine("Các phần tử trong HashSet1");
        foreach (string val in set1)
        {
            Console.WriteLine(val);
        }

        HashSet set2 = new HashSet();
        set2.Add("EF");
        set2.Add("KL");

        Console.WriteLine("Các phần tử trong HashSet2... (Enumerator duyệt qua HashSet)");
        HashSet.Enumerator demoEnum = set2.GetEnumerator();
        while (demoEnum.MoveNext())
        {
            string res = demoEnum.Current;
            Console.WriteLine(res);
        }

        Console.WriteLine("set1 có phải siêu tập của set2? " + set1.IsSupersetOf(set2));
    }
}

Kết quả

Các phần tử trong HashSet1
AB
CD
EF
IJ
KL
OP
Các phần tử trong HashSet2... (Enumerator duyệt qua HashSet)
EF
KL
set1 có phải siêu tập của set2? True

Đặc điểm quan trọng

  • HashSet<T> tự động loại bỏ giá trị trùng lặp.
  • Thứ tự phần tử không được đảm bảo.
  • Hiệu suất truy xuất, thêm, xóa trung bình O(1).
  • Phương thức GetEnumerator() trả về HashSet<T>.Enumerator (struct) để duyệt.