Phương thức ExceptWith() của HashSet<T> loại bỏ tất cả các phần tử có trong tập hợp chỉ định khỏi HashSet hiện tại. Sau khi thực hiện, HashSet chỉ còn chứa các phần tử không tồn tại trong collection tham số.
Ví dụ 1: Xóa các phần tử chung
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main(string[] args)
{
HashSet<string> set1 = new HashSet<string>();
set1.Add("Ryan");
set1.Add("Tom");
set1.Add("Andy");
set1.Add("Tim");
Console.WriteLine("Các phần tử trong HashSet1...");
foreach (string res in set1)
{
Console.WriteLine(res);
}
HashSet<string> set2 = new HashSet<string>();
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("\nCác phần tử trong HashSet2...");
foreach (string res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("\nHashSet1 bằng HashSet2? = " + set1.Equals(set2));
// Xóa khỏi set2 tất cả phần tử có trong set1
set2.ExceptWith(set1);
Console.WriteLine("\nCác phần tử còn lại trong set2 (không có trong set1):");
foreach (string i in set2)
{
Console.WriteLine(i);
}
}
}
Kết quả
Các phần tử trong HashSet1... Ryan Tom Andy Tim Các phần tử trong HashSet2... John Jacob Ryan Tom Andy Tim Steve Mark HashSet1 bằng HashSet2? = False Các phần tử còn lại trong set2 (không có trong set1): John Jacob Steve Mark
Ví dụ 2: set1 lớn hơn set2
using System;
using System.Collections.Generic;
public class Demo
{
public static void Main(string[] args)
{
HashSet<string> set1 = new HashSet<string>();
set1.Add("Jacob");
set1.Add("Ryan");
set1.Add("Tom");
set1.Add("Andy");
set1.Add("Tim");
set1.Add("Steve");
set1.Add("Mark");
Console.WriteLine("Các phần tử trong HashSet1...");
foreach (string res in set1)
{
Console.WriteLine(res);
}
HashSet<string> set2 = new HashSet<string>();
set2.Add("Kevin");
set2.Add("Jacob");
set2.Add("Ryan");
set2.Add("Tom");
set2.Add("Andy");
set2.Add("Tim");
set2.Add("Steve");
set2.Add("Mark");
Console.WriteLine("\nCác phần tử trong HashSet2...");
foreach (string res in set2)
{
Console.WriteLine(res);
}
Console.WriteLine("\nHashSet1 bằng HashSet2? = " + set1.Equals(set2));
set2.ExceptWith(set1);
Console.WriteLine("\nCác phần tử còn lại trong set2:");
foreach (string i in set2)
{
Console.WriteLine(i);
}
}
}
Kết quả
Các phần tử trong HashSet1... Jacob Ryan Tom Andy Tim Steve Mark Các phần tử trong HashSet2... Kevin Jacob Ryan Tom Andy Tim Steve Mark HashSet1 bằng HashSet2? = False Các phần tử còn lại trong set2: Kevin
Các phương thức liên quan của HashSet
| Phương thức | Mô tả | Toán học |
|---|---|---|
ExceptWith(other) |
Loại bỏ phần tử có trong other (hiệu) | A \ B |
IntersectWith(other) |
Giữ lại phần tử chung (giao) | A ∩ B |
UnionWith(other) |
Hợp nhất hai tập hợp (hợp) | A ∪ B |
SymmetricExceptWith(other) |
Giữ phần tử chỉ ở một trong hai (đối xứng) | A Δ B |
IsSubsetOf(other) |
Kiểm tra tập con | A ⊆ B |
IsSupersetOf(other) |
Kiểm tra siêu tập hợp | A ⊇ B |
Lưu ý quan trọng
ExceptWith()thay đổi HashSet hiện tại (in-place), không trả về HashSet mới- Tham số chấp nhận bất kỳ
IEnumerable<T>(List, Array, HashSet...) - Hiệu năng O(n) với n là số phần tử của tham số
- Không ném exception nếu phần tử không tồn tại - bỏ qua lặng lẽ
- Để tạo HashSet mới không ảnh hưởng gốc:
var newSet = set1.Except(set2).ToHashSet()(cần LINQ)