当前位置: 首页>>代码示例>>VB.NET>>正文


VB.NET HashSet<T>构造函数代码示例

本文整理汇总了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
开发者ID:VB.NET开发者,项目名称:System.Collections.Generic,代码行数:11,代码来源:HashSet

示例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
开发者ID:VB.NET开发者,项目名称:System.Collections.Generic,代码行数:28,代码来源:HashSet

示例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
开发者ID:VB.NET开发者,项目名称:System.Collections.Generic,代码行数:67,代码来源:HashSet

输出:

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


注:本文中的System.Collections.Generic.HashSet<T>.HashSet构造函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。