Visual Basic HashSet

In visual basic, Hashset is a generic type of collection and it is useful to represent a set of unique values. The hashset in visual basic will allow storing only the strongly-typed elements i.e. the values of specified data type.

 

In visual basic, the hashset will provide a high performance set operations and the set will return only unique elements and those elements will not be in particular order. Even if we add duplicate elements in hashset, it will return only the unique elements in result set.

 

The size of hashset object will vary dynamically so we can add or remove elements from the hashset based on our requirements.

 

In visual basic, the hashset is not useful when we want to work with duplicate elements in that case, we need to consider using list object in our application.

Visual Basic HashSet Declaration

In visual basic, hashset is a generic type of collection and it is provided by System.Collections.Generic namespace.

 

As discussed, the collection is a class so to define a hashset, we must need to declare an instance of hashset class before we perform any operations such as add, delete, etc. like as shown below.

 

Dim hset As HashSet(Of T)  = New HashSet(Of T)()

If you observe the above hashset declaration, we created a generic hashset (hset) with an instance of hashset class using type parameter (T) as placeholders within the brackets.

 

Here, the brackets (Of T) will indicate that the hashset is a generic type and type parameter T is to represent a type of values to be accepted by hashset.

Visual Basic HashSet Initialization

Following is the example of initializing a generic hashset by specifying a required type to accept values.

 

Dim hset As HashSet(Of Integer)  = New HashSet(Of Integer)()

If you observe the above example, we defined a hashset (hset) with required value type to store. Here, the hashset object will store a value of integer type.

Visual Basic HashSet Properties

Following are the some of commonly used properties of the hashset object in visual basic programming language.

 

PropertyDescription
Count It is used to get the number of elements in hashset.

Visual Basic HashSet Methods

Following are the some of commonly used methods of generic hashset to perform add, search, insert, delete or sort operations in visual basic programming language.

 

MethodDescription
Add It is used to add an elements of specified type to hashset.
Clear It will remove all the elements from hashset.
Contains It is used determine whether the specified element exists in hashset or not.
CopyTo It is used copies an elements of hashset object to an array.
Remove It is used to remove the specified element from hashset.
TryGetValue It is used to search for given value and return if value finds.

Visual Basic HashSet Example

Following is the example of adding and accessing the elements from hashset in visual basic programming langauge.

 

Module Module1

    Sub Main(ByVal args As String())

        ' Create new hashset

        Dim hset As HashSet(Of Integer) = New HashSet(Of Integer)()

        ' Add elements to hashset object

        hset.Add(1)

        hset.Add(2)

        hset.Add(2)

        hset.Add(3)

        hset.Add(3)

        hset.Add(4)

        Console.WriteLine("Number of Elemen in HashSet: {0}", hset.Count)

        Console.WriteLine("*********HashSet Elements********")

        ' Accessing elements from hashset

        For Each item As Integer In hset

            Console.WriteLine(item)

        Next

        ' Creating and initializing hashset

        Dim hset2 As HashSet(Of String) = New HashSet(Of String) From {

                "welcome",

                "to",

                "tutlane",

                "tutlane"

            }

        Console.WriteLine("Number of Elemen in HashSet: {0}", hset2.Count)

        Console.WriteLine("*********HashSet2 Elements********")

        ' Accessing elements

        For Each item As String In hset2

            Console.WriteLine(item)

        Next

        Console.WriteLine("Contains Value '2': {0}", hset.Contains(2))

        Console.WriteLine("Contains Value '10': {0}", hset.Contains(10))

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are able to define a new generic hashset (hset, hset2) collections. Here, we added a defined data type values to the newly created hashsets (hset, hset2) in different ways and we used For Each loop to access the elements from hashset.

 

When we execute above the visual basic program, we will get the result like as shown below.

 

Visual Basic HashSet Example Result

 

If you observe the above result, we got only the unique elements from hashset and it returns the count of unique elements available in the hashset.

Visual Basic HashSet Overview

Following are the important points which we need to remember about hashset in visual basic.

 

  • Hashset is a generic collection and it is useful to represent a set of values.
  • Hashset will allow us to store duplicate values but it will return only unique values in the result set.
  • Hashset collection is not sorted and we can access hashset values by using For Each loop.

Topics Covered