HashSet(Of T).RemoveWhere Method (Predicate(Of T))

 

Removes all elements that match the conditions defined by the specified predicate from a HashSet(Of T) collection.

Namespace:   System.Collections.Generic
Assembly:  System.Core (in System.Core.dll)

Public Function RemoveWhere (
	match As Predicate(Of T)
) As Integer

Parameters

match
Type: System.Predicate(Of T)

The Predicate(Of T) delegate that defines the conditions of the elements to remove.

Return Value

Type: System.Int32

The number of elements that were removed from the HashSet(Of T) collection.

Exception Condition
ArgumentNullException

match is null.

Calling this method is an O(n) operation, where n is Count.

The following example demonstrates how to remove values from a HashSet(Of T) collection using the Remove method. In this example, all odd integers are removed from the HashSet(Of T) collection as specified by the match delegate.

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 }

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: