Phương thức IsSupersetOf() của SortedSet<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. Khác với IsProperSupersetOf(), phương thức này trả về true ngay cả khi hai tập hợp **bằng nhau**.
Định nghĩa
Set A là superset của Set B nếu: A ⊇ B (A chứa B, có thể A = B)
Ví dụ 1: SortedSet<string>
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet<string> set1 = new SortedSet<string>();
set1.Add("CD");
set1.Add("CD"); // Trùng lặp - bị bỏ qua
set1.Add("CD");
set1.Add("CD");
Console.WriteLine("Các phần tử trong SortedSet1...");
foreach (string res in set1)
{
Console.WriteLine(res);
}
SortedSet<string> set2 = new SortedSet<string>();
set2.Add("BC");
set2.Add("CD");
set2.Add("DE");
set2.Add("EF");
set2.Add("AB");
set2.Add("HI");
set2.Add("JK");
Console.WriteLine("\nCác phần tử trong SortedSet2...");
foreach (string res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("\nSortedSet2 là superset của SortedSet1? = " + set2.IsSupersetOf(set1));
}
}
Kết quả
Các phần tử trong SortedSet1... CD Các phần tử trong SortedSet2... AB BC CD DE EF HI JK SortedSet2 là superset của SortedSet1? = True
Ví dụ 2: SortedSet<int> - Hai tập hợp bằng nhau
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main()
{
SortedSet<int> set1 = new SortedSet<int>();
set1.Add(10);
set1.Add(20);
set1.Add(30);
set1.Add(40);
set1.Add(50);
set1.Add(60);
Console.WriteLine("Các phần tử trong SortedSet1...");
foreach (int res in set1)
{
Console.WriteLine(res);
}
SortedSet<int> set2 = new SortedSet<int>();
set2.Add(10);
set2.Add(20);
set2.Add(30);
set2.Add(40);
set2.Add(50);
set2.Add(60);
set2.Add(70);
set2.Add(80);
set2.Add(90);
set2.Add(100);
Console.WriteLine("\nCác phần tử trong SortedSet2...");
foreach (int res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("\nSortedSet2 là superset của SortedSet1? = " + set2.IsSupersetOf(set1));
}
}
Kết quả
Các phần tử trong SortedSet1... 10 20 30 40 50 60 Các phần tử trong SortedSet2... 10 20 30 40 50 60 70 80 90 100 SortedSet2 là superset của SortedSet1? = True
So sánh Superset vs Proper Superset
| Phương thức | Ký hiệu | Trường hợp A = B | Trường hợp A ⊃ B |
|---|---|---|---|
IsSupersetOf |
A ⊇ B | True | True |
IsProperSupersetOf |
A ⊃ B | False | True |
SortedSet<int> A = new SortedSet<int> { 1, 2, 3 };
SortedSet<int> B = new SortedSet<int> { 1, 2, 3 }; // Bằng nhau
SortedSet<int> C = new SortedSet<int> { 1, 2 }; // Con của A
Console.WriteLine(A.IsSupersetOf(B)); // True (A ⊇ B, A = B)
Console.WriteLine(A.IsProperSupersetOf(B)); // False (A ⊃ B, nhưng A = B)
Console.WriteLine(A.IsSupersetOf(C)); // True
Console.WriteLine(A.IsProperSupersetOf(C)); // True
Các phương thức liên quan của SortedSet
| Phương thức | Mô tả | Toán học |
|---|---|---|
IsSupersetOf |
Là siêu tập hợp (có thể bằng) | A ⊇ B |
IsProperSupersetOf |
Là siêu tập hợp chuẩn (phải lớn hơn) | A ⊃ B |
IsSubsetOf |
Là tập con (có thể bằng) | A ⊆ B |
IsProperSubsetOf |
Là tập con chuẩn (phải nhỏ hơn) | A ⊂ B |
Overlaps |
Có phần tử chung | A ∩ B ≠ ∅ |
SetEquals |
Bằng nhau (cùng phần tử) | A = B |
Lưu ý
- Tham số:
IEnumerable<T>- chấp nhận List, Array, HashSet, SortedSet... - Hiệu năng: O(n) với n là số phần tử của tham số (duyệt qua từng phần tử kiểm tra Contains)
- SortedSet tự động sắp xếp và loại trùng lặp
- Key phải implement
IComparable<T>hoặc cung cấpIComparer<T> - Namespace:
System.Collections.Generic