📜  C#|从HashSet中删除集合中的所有元素

📅  最后修改于: 2021-05-29 17:31:45             🧑  作者: Mango

HashSet是唯一元素的无序集合。在System.Collections.Generic命名空间中找到它。它用于我们要防止将重复项插入到集合中的情况。就性能而言,与列表相比更好。哈希集.ExceptWith(IEnumerable ) 方法用于从当前HashSet中移除指定集合中的所有元素目的。

句法:

mySet2.ExceptWith(mySet1)

这里, mySet1mySet2是两个HashSet,函数返回mySet2中不在mySet1中的元素。

异常:如果HashSet为null,则此方法将提供ArgumentNullException

下面给出了一些示例,以更好地理解实现:

范例1:

// C# code to remove all elements
// in a collection from a HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HashSet of integers
        HashSet mySet1 = new HashSet();
  
        // Inserting elements into HashSet mySet1
        for (int i = 0; i < 7; i++) {
            mySet1.Add(i);
        }
  
        // Creating a HashSet of integers
        HashSet mySet2 = new HashSet();
  
        // Inserting elements into HashSet mySet2
        for (int i = 4; i < 11; i++) {
            mySet2.Add(i);
        }
  
        // Removing all elements in a collection from a HashSet
        mySet2.ExceptWith(mySet1);
  
        // Printing the elements in mySet2
        // It only prints the elements which are
        // in mySet2 and not in mySet1
        foreach(int i in mySet2)
        {
            Console.WriteLine(i);
        }
    }
}
输出:
7
8
9
10

范例2:

// C# code to remove all elements
// in a collection from a HashSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HashSet of strings
        HashSet mySet1 = new HashSet();
  
        // Inserting elements into HashSet mySet1
        mySet1.Add("Punjab");
        mySet1.Add("Haryana");
        mySet1.Add("Jammu");
        mySet1.Add("Himachal");
        mySet1.Add("Delhi");
  
        // Displaying all elements in mySet1
        Console.WriteLine("The elements in mySet1 are : ");
  
        foreach(string i in mySet1)
        {
            Console.WriteLine(i);
        }
  
        // Creating a HashSet of strings
        HashSet mySet2 = new HashSet();
  
        // Inserting elements into HashSet mySet2
        mySet2.Add("Bangalore");
        mySet2.Add("Kerala");
        mySet2.Add("Uttar Pradesh");
        mySet2.Add("Himachal");
        mySet2.Add("Delhi");
  
        // Displaying all elements in mySet2
        Console.WriteLine("The elements in mySet2 are : ");
  
        foreach(string i in mySet2)
        {
            Console.WriteLine(i);
        }
  
        // Removing all elements in a collection from a HashSet
        mySet2.ExceptWith(mySet1);
  
        // Printing the elements in mySet2
        // It only prints the elements which are
        // in mySet2 and not in mySet1
  
        Console.WriteLine("The elements in mySet2 are : ");
  
        foreach(string i in mySet2)
        {
            Console.WriteLine(i);
        }
    }
}
输出:
The elements in mySet1 are : 
Punjab
Haryana
Jammu
Himachal
Delhi
The elements in mySet2 are : 
Bangalore
Kerala
Uttar Pradesh
Himachal
Delhi
The elements in mySet2 are : 
Bangalore
Kerala
Uttar Pradesh

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.hashset-1.exceptwith?view=netframework-4.7.2