HashSet<T>.RemoveWhere(Predicate<T>) HashSet<T>.RemoveWhere(Predicate<T>) HashSet<T>.RemoveWhere(Predicate<T>) HashSet<T>.RemoveWhere(Predicate<T>) Method

定义

HashSet<T> 集合中移除与指定的谓词所定义的条件相匹配的所有元素。Removes all elements that match the conditions defined by the specified predicate from a HashSet<T> collection.

public:
 int RemoveWhere(Predicate<T> ^ match);
public int RemoveWhere (Predicate<T> match);
member this.RemoveWhere : Predicate<'T> -> int
Public Function RemoveWhere (match As Predicate(Of T)) As Integer

参数

match
Predicate<T> Predicate<T> Predicate<T> Predicate<T>

Predicate<T> 委托,用于定义要移除的元素应满足的条件。The Predicate<T> delegate that defines the conditions of the elements to remove.

返回

已从 HashSet<T> 集合中移除的元素的个数。The number of elements that were removed from the HashSet<T> collection.

异常

示例

下面的示例演示如何HashSet<T> Remove使用方法从集合中删除值。The following example demonstrates how to remove values from a HashSet<T> collection using the Remove method. 在此示例中,将从HashSet<T>集合中移除match由委托指定的所有奇数整数。In this example, all odd integers are removed from the HashSet<T> collection as specified by the match delegate.

HashSet<int> numbers = new HashSet<int>();

for (int i = 0; i < 20; i++) {
    numbers.Add(i);
}

// Display all the numbers in the hash table.
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);

// Remove all odd numbers.
numbers.RemoveWhere(IsOdd);
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);

// Check if the hash table contains 0 and, if so, remove it.
if (numbers.Contains(0)) {
    numbers.Remove(0);
}
Console.Write("numbers contains {0} elements: ", numbers.Count);
DisplaySet(numbers);

bool IsOdd(int i)
{
    return ((i % 2) == 1);
}

void DisplaySet(HashSet<int> set)
{
    Console.Write("{");
    foreach (int i in set)
        Console.Write(" {0}", i);

    Console.WriteLine(" }");
}

// This example display the following output:
//    numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
//    numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
//    numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }
Imports System.Collections.Generic

Module Example
    Public Sub Main()
        Dim numbers As New HashSet(Of Integer)()

        For i As Integer = 0 To 19
            numbers.Add(i)
        Next i

        ' Display all the numbers in the hash table.
        Console.Write("numbers contains {0} elements: ", numbers.Count)
        DisplaySet(numbers)

        ' Remove all odd numbers.
        numbers.RemoveWhere(AddressOf IsOdd)
        Console.Write("numbers contains {0} elements: ", numbers.Count)
        DisplaySet(numbers)

        ' Check if the hash table contains 0 and, if so, remove it.
        If numbers.Contains(0) Then
            numbers.Remove(0)
        End If
        Console.Write("numbers contains {0} elements: ", numbers.Count)
        DisplaySet(numbers)
    End Sub

    Private Function IsOdd(ByVal i As Integer) As Boolean
        Return ((i Mod 2) = 1)
    End Function

    Private Sub DisplaySet(ByVal coll As HashSet(Of Integer))
        Console.Write("{")
        For Each i As Integer In coll
            Console.Write(" {0}", i)
        Next
        Console.WriteLine(" }")
    End Sub
End Module
' The example displays the following output:
'    numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
'    numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
'    numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }

注解

调用此方法的运算复杂度为nO (), n其中Count,为。Calling this method is an O(n) operation, where n is Count.

适用于