Phương thức IsProperSupersetOf() của HashSet<T> kiểm tra xem tập hợp hiện tại có phải là siêu tập hợp chuẩn (proper superset) của tập hợp chỉ định hay không. Điều này nghĩa là: tập hợp hiện tại chứa tất cả phần tử của tập hợp chỉ định VÀ có ít nhất một phần tử không nằm trong tập hợp chỉ định.
Định nghĩa
Set A là proper superset của Set B nếu: A ⊃ B (A chứa B và A ≠ B)
Ví dụ 1: HashSet<int>
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
HashSet<int> set1 = new HashSet<int>();
set1.Add(30);
set1.Add(60);
set1.Add(70);
set1.Add(80);
set1.Add(100);
set1.Add(125);
set1.Add(150);
set1.Add(200);
Console.WriteLine("Các phần tử trong HashSet1:");
foreach (int val in set1)
{
Console.WriteLine(val);
}
HashSet<int> set2 = new HashSet<int>();
set2.Add(30);
set2.Add(60);
set2.Add(70);
Console.WriteLine("\nCác phần tử trong HashSet2:");
foreach (int val in set2)
{
Console.WriteLine(val);
}
Console.WriteLine("\nset1 là proper superset của set2? " + set1.IsProperSupersetOf(set2));
}
}
Kết quả
Các phần tử trong HashSet1: 30 60 70 80 100 125 150 200 Các phần tử trong HashSet2: 30 60 70 set1 là proper superset của set2? True
Ví dụ 2: HashSet<string>
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("GH");
set1.Add("IJ");
set1.Add("KL");
set1.Add("MN");
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à proper superset của set2? " + set1.IsProperSupersetOf(set2));
}
}
Kết quả
Các phần tử trong HashSet1: AB CD EF GH IJ KL MN OP Các phần tử trong HashSet2: EF KL set1 là proper superset của set2? True
So sánh các phương thức liên quan
| Phương thức | Ý nghĩa | Ví dụ: A={1,2,3}, B={1,2} |
|---|---|---|
IsSupersetOf |
Siêu tập hợp (có thể bằng nhau) | True (A ⊇ B) |
IsProperSupersetOf |
Siêu tập hợp chuẩn (phải lớn hơn) | True (A ⊃ B) |
IsSubsetOf |
Tập con (có thể bằng nhau) | False |
IsProperSubsetOf |
Tập con chuẩn (phải nhỏ hơn) | False |
Overlaps |
Có phần tử chung | True |
SetEquals |
Bằng nhau (cùng phần tử) | False |
Các trường hợp trả về False
HashSet<int> A = new HashSet<int> { 1, 2, 3 };
HashSet<int> B = new HashSet<int> { 1, 2, 3 }; // Bằng nhau
HashSet<int> C = new HashSet<int> { 1, 2, 4 }; // Có phần tử không thuộc A
Console.WriteLine(A.IsProperSupersetOf(B)); // False - bằng nhau
Console.WriteLine(A.IsProperSupersetOf(C)); // False - C có 4 không trong A
Lưu ý
- Phương thức nhận
IEnumerable<T>- có thể truyền List, Array, HashSet khác... - Trả về
falsenếu hai tập hợp bằng nhau - Hiệu năng O(n) với n là số phần tử của tập hợp tham số
- HashSet không giữ thứ tự và không cho phép trùng lặp