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 a specified data type.
In visual basic, the HashSet will provide high-performance set operations and the set will return only unique elements and those elements will not be in a particular order. Even if we add duplicate elements in hashset, it will return only the unique elements in the 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 objects in our application.
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 just 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.
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 the required value type to store. Here, the hashset object will store a value of integer type.
The following are some of the commonly used properties of the hashset object in a visual basic programming language.
Property | Description |
---|---|
Count | It is used to get the number of elements in hashset. |
The following are some of the commonly used methods of generic hashset to perform add, search, insert, delete or sort operations in visual basic programming language.
Method | Description |
---|---|
Add | It is used to add elements of specified type to hashset. |
Clear | It will remove all the elements from hashset. |
Contains | It is used to determine whether the specified element exists in HashSet or not. |
CopyTo | It is used to copy the elements of the HashSet object to an array. |
Remove | It is used to remove the specified element from hashset. |
TryGetValue | It is used to search for a given value and return if value finds. |
Following is the example of adding and accessing the elements from hashset in a visual basic programming language.
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.
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.
The following are the important points which we need to remember about HashSet in visual basic.