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

Xóa tất cả phần tử khỏi HashSet trong C#

Để xóa tất cả các phần tử khỏi HashSet<T> trong C#, bạn sử dụng phương thức Clear(). Phương thức này loại bỏ tất cả phần tử và đặt Count về 0. HashSet<T> là tập hợp không có thứ tự, không cho phép trùng lặp.

Ví dụ 1: Xóa tất cả từ HashSet và so sánh

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(string[] args)
    {
        HashSet<string> set1 = new HashSet<string>();
        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<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("HashSet1 bằng HashSet2? = " + set1.Equals(set2));
        Console.WriteLine("Số lượng HashSet2 = " + set2.Count);

        // Xóa tất cả phần tử
        set2.Clear();

        Console.WriteLine("Số lượng HashSet2 (đã cập nhật) = " + 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...
John
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
HashSet1 bằng HashSet2? = False
Số lượng HashSet2 = 8
Số lượng HashSet2 (đã cập nhật) = 0

Ví dụ 2: HashSet đơn giản

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main(string[] args)
    {
        HashSet<string> set1 = new HashSet<string>();
        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 HashSet...");
        foreach (string res in set1)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("Số lượng HashSet1 = " + set1.Count);

        set1.Clear();

        Console.WriteLine("Số lượng HashSet1 (đã cập nhật) = " + set1.Count);
    }
}

Kết quả

Các phần tử trong HashSet...
A
B
C
D
E
F
G
H
Số lượng HashSet1 = 8
Số lượng HashSet1 (đã cập nhật) = 0

Đặc điểm của HashSet<T>

Đặc điểm Mô tả
Trùng lặp Không cho phép
Thứ tự Không bảo đảm (unordered)
Cấu trúc Bảng băm (Hash Table)
Phức tạp Add/Remove/Contains O(1) trung bình
Clear() O(n) - xóa tất cả bucket

Các phương thức xóa khác

Phương thức Mô tả Trả về
Clear() Xóa tất cả phần tử void
Remove(T item) Xóa một phần tử cụ thể bool - true nếu xóa thành công
RemoveWhere(Predicate<T>) Xóa phần tử thỏa điều kiện int - số phần tử xóa
ExceptWith(IEnumerable<T>) Xóa phần tử có trong collection khác void
IntersectWith(IEnumerable<T>) Giữ lại phần tử chung void
SymmetricExceptWith(IEnumerable<T>) Giữ phần tử chỉ ở một tập void

Ví dụ RemoveWhere

HashSet<int> set = new HashSet<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Xóa số chẵn
int removed = set.RemoveWhere(x => x % 2 == 0);
Console.WriteLine($"Đã xóa {removed} phần tử");
// Kết quả: 1, 3, 5, 7, 9

Clear() vs Tạo mới

HashSet<int> set = new HashSet<int> { 1, 2, 3, 4, 5 };

// Cách 1: Clear() - giữ đối tượng
set.Clear();
// Count = 0, Capacity nội bộ không đổi

// Cách 2: Tạo mới - giải phóng bộ nhớ
set = new HashSet<int>();
// Đối tượng cũ bị GC thu gom

Lưu ý

  • Clear() không ném exception nếu HashSet đã rỗng
  • Không giải phóng bộ nhớ nội bộ (buckets array)
  • HashSet<T> nằm trong System.Collections.Generic
  • Type-safe, hiệu năng tốt hơn HashSet non-generic
  • Cần override GetHashCode() và Equals() đúng cho custom type