本文整理汇总了VB.NET中System.Collections.Generic.HashSet<T>.HashSet构造函数的典型用法代码示例。如果您正苦于以下问题:VB.NET HashSet<T>构造函数的具体用法?VB.NET HashSet<T>怎么用?VB.NET HashSet<T>使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Collections.Generic.HashSet<T>
的用法示例。
在下文中一共展示了HashSet<T>构造函数的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: HashSet
Dim evenNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
Dim oddNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
For i As Integer = 0 To 4
' Populate evenNumbers with only even numbers.
evenNumbers.Add(i * 2)
' Populate oddNumbers with only odd numbers.
oddNumbers.Add((i * 2) + 1)
Next i
示例2: Main
Shared Sub Main()
Dim evenNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
Dim oddNumbers As HashSet(Of Integer) = New HashSet(Of Integer)()
For i As Integer = 0 To 4
' Populate evenNumbers with only even numbers.
evenNumbers.Add(i * 2)
' Populate oddNumbers with only odd numbers.
oddNumbers.Add((i * 2) + 1)
Next i
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count)
DisplaySet(evenNumbers)
Console.Write("oddNumbers contains {0} elements: ", oddNumbers.Count)
DisplaySet(oddNumbers)
' Create a new HashSet populated with even numbers.
Dim numbers As HashSet(Of Integer) = New HashSet(Of Integer)(evenNumbers)
Console.WriteLine("numbers UnionWith oddNumbers...")
numbers.UnionWith(oddNumbers)
Console.Write("numbers contains {0} elements: ", numbers.Count)
DisplaySet(numbers)
End Sub
示例3: Program
' 导入命名空间
Imports System.Collections.Generic
Class Program
Public Shared Sub Main()
Dim allVehicles As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
Dim someVehicles As New List(Of String)()
someVehicles.Add("Planes")
someVehicles.Add("Trains")
someVehicles.Add("Automobiles")
' Add in the vehicles contained in the someVehicles list.
allVehicles.UnionWith(someVehicles)
Console.WriteLine("The current HashSet contains:" + vbNewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
allVehicles.Add("Ships")
allVehicles.Add("Motorcycles")
allVehicles.Add("Rockets")
allVehicles.Add("Helicopters")
allVehicles.Add("Submarines")
Console.WriteLine(vbNewLine + "The updated HashSet contains:" + vbNewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
' Verify that the 'All Vehicles' set contains at least the vehicles in
' the 'Some Vehicles' list.
If allVehicles.IsSupersetOf(someVehicles) Then
Console.Write(vbNewLine + "The 'All' vehicles set contains everything in ")
Console.WriteLine("'Some' vechicles list.")
End If
' Check for Rockets. Here the OrdinalIgnoreCase comparer will compare
' True for the mixed-case vehicle type.
If allVehicles.Contains("roCKeTs") Then
Console.WriteLine(vbNewLine + "The 'All' vehicles set contains 'roCKeTs'")
End If
allVehicles.ExceptWith(someVehicles)
Console.WriteLine(vbNewLine + "The excepted HashSet contains:" + vbNewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
' Remove all the vehicles that are not 'super cool'.
allVehicles.RemoveWhere(AddressOf isNotSuperCool)
Console.WriteLine(vbNewLine + "The super cool vehicles are:" + vbNewLine)
For Each vehicle As String In allVehicles
Console.WriteLine(vehicle)
Next vehicle
End Sub
' Predicate to determine vehicle 'coolness'.
Private Shared Function isNotSuperCool(vehicle As String) As Boolean
Dim notSuperCool As Boolean = _
(vehicle <> "Helicopters") And (vehicle <> "Motorcycles")
Return notSuperCool
End Function
End Class
输出:
The current HashSet contains: Planes Trains Automobiles The updated HashSet contains: Planes Trains Automobiles Ships Motorcycles Rockets Helicopters Submarines The 'All' vehicles set contains everything in 'Some' vechicles list. The 'All' vehicles set contains 'roCKeTs' The excepted HashSet contains: Ships Motorcycles Rockets Helicopters Submarines The super cool vehicles are: Motorcycles Helicopters