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

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

เมธอด ExceptWith(IEnumerable<T> other) ของ HashSet<T> ใช้ลบองค์ประกอบที่อยู่ใน other ออกจาก HashSet ปัจจุบัน (In-place) ทำให้เหลือเฉพาะสมาชิกที่อยู่ใน HashSet เดิมแต่ไม่อยู่ใน other

ตัวอย่างที่ 1: ลบสมาชิกซ้ำกันระหว่างสอง HashSet

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("องค์ประกอบใน 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));

        // ลบสมาชิกที่อยู่ใน set1 ออกจาก set2 (In-place)
        set2.ExceptWith(set1);

        // แสดงสมาชิกที่เหลือใน set2 (คือสมาชิกที่อยู่ใน set2 แต่ไม่อยู่ใน set1)
        Console.WriteLine("สมาชิกที่เหลือใน set2 หลัง ExceptWith:");
        foreach (string i in set2)
        {
            Console.WriteLine(i);
        }
    }
}

ผลลัพธ์

องค์ประกอบใน HashSet1...
Ryan
Tom
Andy
Tim
องค์ประกอบใน HashSet2...
John
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
HashSet1 เท่ากับ HashSet2? = False
John
Jacob
Steve
Mark

ตัวอย่างที่ 2: กรณี set1 มีสมาชิกมากกว่า

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

        Console.WriteLine("HashSet1 เท่ากับ HashSet2? = " + set1.Equals(set2));

        // ลบสมาชิกที่อยู่ใน set1 ออกจาก set2
        set2.ExceptWith(set1);

        // เหลือแค่ "Kevin" ที่อยู่ใน set2 แต่ไม่อยู่ใน set1
        foreach (string i in set2)
        {
            Console.WriteLine(i);
        }
    }
}

ผลลัพธ์

องค์ประกอบใน HashSet1...
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
องค์ประกอบใน HashSet2...
Kevin
Jacob
Ryan
Tom
Andy
Tim
Steve
Mark
HashSet1 เท่ากับ HashSet2? = False
Kevin

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

เมธอดคำอธิบายตัวดำเนินการเซต
UnionWithรวม (Union) - เพิ่มสมาชิกจาก other เข้ามาA ∪ B
IntersectWithเก็บเฉพาะสมาชิกร่วม (Intersection)A ∩ B
ExceptWithลบสมาชิกที่อยู่ใน other (Difference)A \ B
SymmetricExceptWithเก็บสมาชิกที่อยู่ในเซตใดเซตหนึ่งอย่างเดียว (Symmetric Difference)A Δ B
IsSubsetOfตรวจสอบว่าเป็นสับเซตหรือไม่A ⊆ B
IsSupersetOfตรวจสอบว่าเป็นซูเปอร์เซตหรือไม่A ⊇ B
IsProperSubsetOf / IsProperSupersetOfตรวจสอบโพรเพอร์ (ไม่เท่ากัน)A ⊂ B / A ⊃ B
Overlapsมีสมาชิกร่วมกันอย่างน้อย 1 ตัวA ∩ B ≠ ∅
SetEqualsสมาชิกเหมือนกันทุกตัวA = B

ข้อสังเกต

  • ExceptWith แก้ไข HashSet เดิม In-place (ไม่คืนค่า HashSet ใหม่)
  • หากต้องการเก็บต้นฉบับ ให้ Clone ก่อน: var copy = new HashSet<T>(original); copy.ExceptWith(other);
  • HashSet ไมอนุญาตค่าซ้ำ และไม่รับประกันลำดับ