📜  C#|使用谓词定义的条件从HashSet中删除元素

📅  最后修改于: 2021-05-29 13:52:30             🧑  作者: Mango

HashSet是唯一元素的无序集合。它位于System.Collections.Generic命名空间下。它用于我们要防止将重复项插入到集合中的情况。就性能而言,与列表相比更好。

HashSet .RemoveWhere(Predicate )方法用于从HashSet 集合中删除所有与指定谓词定义的条件相匹配的元素。

句法:

public int RemoveWhere (Predicate match);

返回值:该方法返回从HashSet 集合中删除的元素数。

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

注意:调用此方法是一个O(n)操作,其中n是Count,即集合中包含的元素数。

下面的程序说明了HashSet .RemoveWhere(Predicate )方法的用法:

范例1:

// C# code to remove elements from a HashSet
// with conditions defined by the predicate
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HashSet of integers
        HashSet mySet = new HashSet();
  
        // Inserting elements into HashSet
        for (int i = 0; i < 10; i++) {
            mySet.Add(i);
        }
  
        Console.WriteLine("The elements in HashSet are : ");
  
        // Displaying the elements in HashSet
        foreach(int i in mySet)
        {
            Console.WriteLine(i);
        }
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
  
        // Remove elements from a HashSet
        // with conditions defined by the predicate
        mySet.RemoveWhere(isEven);
  
        Console.WriteLine("The elements in HashSet are : ");
  
        // Displaying the elements in HashSet
        foreach(int i in mySet)
        {
            Console.WriteLine(i);
        }
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
    }
  
    // Helper function which tells
    // whether an element is even or not
    private static bool isEven(int i)
    {
        return ((i % 2) == 0);
    }
}
输出:
The elements in HashSet are : 
0
1
2
3
4
5
6
7
8
9
Number of elements are : 10
The elements in HashSet are : 
1
3
5
7
9
Number of elements are : 5

范例2:

// C# code to remove elements from a HashSet
// with conditions defined by the predicate
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HashSet of integers
        HashSet mySet = new HashSet();
  
        // Inserting elements into HashSet
        for (int i = 0; i < 20; i++) {
            mySet.Add(i);
        }
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
  
        // Remove elements from a HashSet
        // with conditions defined by the predicate
        mySet.RemoveWhere(myFunc);
  
        // Displaying the number of elements in HashSet
        Console.WriteLine("Number of elements are : " + mySet.Count);
    }
  
    // Helper function which tells
    // whether an element is divisible
    // by both 2 and 3
    private static bool myFunc(int i)
    {
        return ((i % 2) == 0 && (i % 3 == 0));
    }
}
输出:
Number of elements are : 20
Number of elements are : 16

参考:

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