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

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

Để xóa tất cả các phần tử khỏi SortedSet<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ả các phần tử và đặt Count về 0, nhưng không thay đổi dung lượng nội bộ.

Ví dụ 1: Xóa tất cả từ SortedSet<string>

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet<string> set1 = new SortedSet<string>();
        set1.Add("AB");
        set1.Add("BC");
        set1.Add("CD");
        set1.Add("EF");

        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("Số lượng phần tử trong SortedSet = " + set2.Count);

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

        Console.WriteLine("Số lượng phần tử trong SortedSet (đã cập nhật) = " + set2.Count);
    }
}

Kết quả

Các phần tử trong SortedSet1...
AB
BC
CD
EF

Các phần tử trong SortedSet2...
AB
BC
CD
DE
EF
HI
JK
Số lượng phần tử trong SortedSet = 7
Số lượng phần tử trong SortedSet (đã cập nhật) = 0

Ví dụ 2: SortedSet<int>

using System;
using System.Collections.Generic;

public class Demo
{
    public static void Main()
    {
        SortedSet<int> set1 = new SortedSet<int>();
        set1.Add(100);
        set1.Add(200);
        set1.Add(300);
        set1.Add(400);

        Console.WriteLine("Các phần tử trong SortedSet...");
        foreach (int res in set1)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("Số lượng phần tử trong SortedSet = " + set1.Count);

        set1.Clear();

        Console.WriteLine("Số lượng phần tử trong SortedSet (đã cập nhật) = " + set1.Count);
    }
}

Kết quả

Các phần tử trong SortedSet...
100
200
300
400
Số lượng phần tử trong SortedSet = 4
Số lượng phần tử trong SortedSet (đã cập nhật) = 0

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

Đặc điểm Mô tả
Sắp xếp Tự động sắp xếp tăng dần (IComparable<T>)
Trùng lặp Không cho phép
Cấu trúc Cây cân bằng (Red-Black Tree)
Phức tạp O(log n) cho Add, Remove, Contains
Clear() O(n) - xóa tất cả node

So sánh Clear() với new SortedSet()

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

// Cách 1: Clear() - giữ lại đối tượng, xóa dữ liệu
set.Clear();
// set.Count = 0, Capacity không đổi

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

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

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

// Xóa một phần tử cụ thể
bool removed = set.Remove(3); // Trả về true nếu xóa thành công

// Xóa theo điều kiện (C# 6+)
set.RemoveWhere(x => x > 3); // Xóa 4, 5

// Xóa min/max
set.Remove(set.Min); // Xóa phần tử nhỏ nhất
set.Remove(set.Max); // Xóa phần tử lớn nhất

Lưu ý

  • Clear() không giải phóng bộ nhớ nội bộ (capacity)
  • SortedSet duy trì thứ tự sắp xếp tự động
  • Không hỗ trợ truy xuất theo index (không có [index])
  • Cần using System.Collections.Generic
  • Type-safe hơn SortedList non-generic