Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

ลบองค์ประกอบทั้งหมดจาก HashSet ใน C#

เมธอด Clear() ของ HashSet<T> ใช้ลบองค์ประกอบทั้งหมด ทำให้ชุดข้อมูลว่างเปล่า (Count = 0) แต่ยังคงออบเจ็กต์ไว้ใช้งานต่อได้

ตัวอย่างที่ 1: HashSet สองชุด เปรียบเทียบและ Clear

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("องค์ประกอบใน 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("องค์ประกอบใน HashSet2...");
        foreach (string res in set2)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("HashSet1 เท่ากับ HashSet2? = " + set1.Equals(set2)); // False
        Console.WriteLine("จำนวนใน HashSet2 = " + set2.Count); // 8

        // ลบทั้งหมดใน set2
        set2.Clear();

        Console.WriteLine("จำนวนใน HashSet2 (อัปเดต) = " + set2.Count); // 0
    }
}

ผลลัพธ์

องค์ประกอบใน HashSet1...
A
B
C
D
E
F
G
H
องค์ประกอบใน HashSet2...
John
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
HashSet1 เท่ากับ HashSet2? = False
จำนวนใน HashSet2 = 8
จำนวนใน HashSet2 (อัปเดต) = 0

ตัวอย่างที่ 2: HashSet เดียว Clear

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("องค์ประกอบใน HashSet...");
        foreach (string res in set1)
        {
            Console.WriteLine(res);
        }

        Console.WriteLine("จำนวนใน HashSet1 = " + set1.Count); // 8

        set1.Clear();

        Console.WriteLine("จำนวนใน HashSet1 (อัปเดต) = " + set1.Count); // 0
    }
}

ผลลัพธ์

องค์ประกอบใน HashSet...
A
B
C
D
E
F
G
H
จำนวนใน HashSet1 = 8
จำนวนใน HashSet1 (อัปเดต) = 0

เมธอดหลักของ HashSet<T> สรุป

เมธอดคำอธิบายComplexity
Add(T)เพิ่มสมาชิก (คืน true ถ้าเพิ่มใหม่ false ถ้ามีอยู่แล้ว)O(1) Average
Remove(T)ลบสมาชิกที่ระบุO(1) Average
Clear()ลบสมาชิกทั้งหมดO(n)
Contains(T)ตรวจสอบว่ามีสมาชิกหรือไม่O(1) Average
CountจำนวนสมาชิกO(1)
UnionWithรวมกับอีกชุด (Union)O(m)
IntersectWithเก็บเฉพาะสมาชิกร่วม (Intersection)O(m)
ExceptWithลบสมาชิกที่อยู่ในอีกชุด (Difference)O(m)
SymmetricExceptWithเก็บสมาชิกที่อยู่ในชุดใดชุดหนึ่งอย่างเดียวO(m)

ข้อสังเกต

  • HashSet<T> ไม่อนุญาตค่าซ้ำ และไม่รับประกันลำดับ
  • ใช้ Hash Table ภายใน ให้ O(1) สำหรับ Add, Remove, Contains (Average Case)
  • Clear() เป็น O(n) ต้องเดินทุก Bucket เพื่อลบ Reference
  • หลัง Clear() สามารถ Add ใหม่ได้ทันที Capacity อาจคงอยู่ (เพื่อประสิทธิภาพ)
  • เหมาะสำหรับ: ตรวจสอบความซ้ำ, เซตอินเตอร์เฟซ, ความแตกต่างของชุดข้อมูล