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

Kiểm tra HashSet là siêu tập hợp của collection trong C#

Phương thức IsSupersetOf() của HashSet<T> kiểm tra xem tập hợp hiện tại có chứa **tất cả** phần tử của tập hợp chỉ định hay không. Trả về true nếu HashSet hiện tại là siêu tập hợp (có thể bằng nhau) của collection tham số.

Định nghĩa

Set A là superset của Set B nếu: A ⊇ B (A chứa mọi phần tử của B, có thể A = B)

Ví dụ 1: HashSet là superset

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        HashSet<string> set1 = new HashSet<string>();
        set1.Add("AB");
        set1.Add("CD");
        set1.Add("EF");
        set1.Add("AB"); // Trùng - bỏ qua
        set1.Add("IJ");
        set1.Add("KL");
        set1.Add("EF"); // Trùng - bỏ qua
        set1.Add("OP");

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

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

        Console.WriteLine("\nCác phần tử trong HashSet2:");
        foreach (string val in set2)
        {
            Console.WriteLine(val);
        }

        Console.WriteLine("\nset1 là superset 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:
EF
KL
set1 là superset của set2? True

Ví dụ 2: HashSet KHÔNG phải superset

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        HashSet<string> set1 = new HashSet<string>();
        set1.Add("IJ");
        set1.Add("KL");
        set1.Add("EF");
        set1.Add("OP");

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

        HashSet<string> set2 = new HashSet<string>();
        set2.Add("KL");
        set2.Add("MN");
        set2.Add("OP");
        set2.Add("QR");

        Console.WriteLine("\nCác phần tử trong HashSet2:");
        foreach (string val in set2)
        {
            Console.WriteLine(val);
        }

        Console.WriteLine("\nset1 là superset của set2? " + set1.IsSupersetOf(set2));
    }
}

Kết quả

Các phần tử trong HashSet1:
IJ
KL
EF
OP

Các phần tử trong HashSet2:
KL
MN
OP
QR
set1 là superset của set2? False

Phân tích ví dụ 2

  • set1 có: {IJ, KL, EF, OP}
  • set2 có: {KL, MN, OP, QR}
  • set1 **thiếu** MN và QR so với set2
  • Do đó set1 **KHÔNG** phải superset của set2

So sánh các phương thức tập hợp

Phương thức Ý nghĩa Ký hiệu A={1,2,3}, B={1,2}
IsSupersetOf Siêu tập hợp (có thể bằng) A ⊇ B True
IsProperSupersetOf Siêu tập hợp chuẩn (phải lớn hơn) A ⊃ B True
IsSubsetOf Tập con (có thể bằng) A ⊆ B False
IsProperSubsetOf Tập con chuẩn (phải nhỏ hơn) A ⊂ B False
Overlaps Có giao nhau A ∩ B ≠ ∅ True
SetEquals Bằng nhau A = B False

Khi A = B

HashSet<int> A = new HashSet<int> { 1, 2, 3 };
HashSet<int> B = new HashSet<int> { 1, 2, 3 };

Console.WriteLine(A.IsSupersetOf(B));           // True
Console.WriteLine(A.IsProperSupersetOf(B));     // False
Console.WriteLine(A.SetEquals(B));              // True

Lưu ý quan trọng

  • Tham số nhận IEnumerable<T> - linh hoạt (List, Array, HashSet...)
  • Hiệu năng O(n) với n = số phần tử của tham số (kiem tra Contains cho mỗi phần tử)
  • HashSet sử dụng bảng băm - Contains O(1) trung bình
  • Không sửa đổi HashSet gốc (read-only operation)
  • Cần using System.Collections.Generic

Ví dụ thực tế: Kiểm tra quyền hạn

HashSet<string> userPermissions = new HashSet<string> 
{ 
    "Read", "Write", "Delete", "Admin", "Export" 
};

HashSet<string> requiredPermissions = new HashSet<string> 
{ 
    "Read", "Write" 
};

if (userPermissions.IsSupersetOf(requiredPermissions))
{
    Console.WriteLine("User có đủ quyền thực hiện thao tác");
}
else
{
    Console.WriteLine("User THIẾU quyền");
}