Create HashSet(Of T) class from another collection : HashSet « Data Structure « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Data Structure » HashSetScreenshots 
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 StringAs Boolean
        Return s1.Equals(s2, StringComparison.CurrentCultureIgnoreCase)
    End Function

    Public Overrides Function GetHashCode(As StringAs Integer
        return MyBase.GetHashCode()
    End Function
End Class

   
  
Related examples in the same category
1.KeyedCollection(TKey, TItem) is the abstract base class for a collection whose keys are embedded in the values.
2.Lookup(TKey, TElement) Represents a collection of keys each mapped to one or more values.
3.ReadOnlyCollection(T) Class Provides the base class for a generic read-only collection.
4.HashSet(T) Class Represents a set of values.
5.Create a HashSet from another HashSet
6.Create HashSet(Of T) class that uses the default equality comparer for the set type
7.Remove element from HashSet with condition function
8.HashSet(T).Clear Method removes all elements from a HashSet(Of T) object.
9.HashSet(T).Contains Method tells whether a HashSet(Of T) object contains the specified element.
10.HashSet(T).ExceptWith Method removes all elements in the specified collection from the current HashSet(Of T) object.
11.HashSet(T).IsProperSubsetOf Method tells whether a HashSet(Of T) object is a proper subset of the specified collection.
12.HashSet(T).SymmetricExceptWith Method keeps elements that are present either in that object or in the specified collection, but not both.
ww__w.jav___a2_s__.c___o_m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.