Remove element from HashSet with condition function
Imports System
Imports System.Collections.Generic
Class Program
Public Shared Sub Main()
Dim compareVehicles As New NumberComparer()
Dim allVehicles As New HashSet(Of String)(compareVehicles)
Dim someVehicles As New List(Of String)()
someVehicles.Add("One")
someVehicles.Add("Two")
someVehicles.Add("Three")
allVehicles.RemoveWhere(AddressOf isNotWhatIWant)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
End Sub
Private Shared Function isNotWhatIWant(ByVal vehicle As String) As Boolean
Dim notWhatIWant As Boolean = (vehicle <> "One") And (vehicle <> "Two")
Return notWhatIWant
End Function
End Class
Class NumberComparer
Inherits EqualityComparer(Of String)
Public Overrides Function Equals(ByVal s1 As String, ByVal s2 As String) As Boolean
Return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase)
End Function
Public Overrides Function GetHashCode(ByVal s As String) As Integer
Return MyBase.GetHashCode()
End Function
End Class
Related examples in the same category