本文整理匯總了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