Create HashSet(Of T) class from another collection
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.UnionWith(someVehicles)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
allVehicles.Add("Four")
allVehicles.Add("Five")
allVehicles.Add("One")
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
Console.WriteLine(allVehicles.IsSupersetOf(someVehicles))
Console.WriteLine(allVehicles.Contains("One"))
allVehicles.ExceptWith(someVehicles)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
End Sub
End Class
Class NumberComparer
Inherits EqualityComparer(Of String)
Public Overrides Function Equals(s1 As String, s2 As String) As Boolean
Return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase)
End Function
Public Overrides Function GetHashCode(s As String) As Integer
return MyBase.GetHashCode()
End Function
End Class
Related examples in the same category