Hash Table
Working with HashTable
Introduction
Collection represents a set of objects that can be access by stepping through each element in turn.
What is the big deal ?. Even an array can hold a set of objects.
True, but we have more flexibility when we use a collection object when compared to arrays.
-
Array are of fixed size, yes, it can be resized using Redim and Preserve. But for a Collection we can keep on adding elements to it.
-
Arrays can store only one data type(Other that object array) whereas collections can hold any objects.
-
Accessing the element is very simple and very fast.
-
Removing the element in Collection is very simple, in the case of arrays we need to shit the entire set of value up and reduce the size.
We have many collection class available in .Net like ArrayList, HashTable etc., in the System.Collections Namespace.
In this section of article we will see more on HashTable.
HashTable
Represents a collection of key-and-value pairs that are organized based on the hash code of the key.
Constructor of HashTable
We have ten overloaded Contructor. we will see some important constructors of HashTable.
-
Constructor with no parameters - Creates an empty HashTable.
- Constructor with an Integer Parameter - Creates an empty hashtable, where the parameter represents the approximate number of element that the HashTable can have initially.
Adding an element to the HashTable
Adding an element to the hash table is very simple. It is done through Add method which takes the two parameter key and the value. Where the key represents the key which will be used to access the element and value is of type object which is the actual element to tbe stored.
Syntax
{hash table object}.Add(Key as Object, value as Object)
Accessing an element in the HashTable
Accesing the element in a HashTable is very simple. It can be accessed through the key which is given at the time of adding the element.
It is accessed through the parameterised default property item which takes an Key of type object and returns object contained in the HashTable with the specified key.
Syntax
{hash table object}.Item({key})
Or
{hash table object}({Key})
Count Property
The count property is used to get the size of the HashTable.
Searching for an element
It is very simple to search an object in the HashTable. We can search for an element using the Key associated with the object using the Contains methods which takes the key as the parameter and return boolean value(true representing the key is found, false representing the key is not found).
Clear Method
The clear method is used to clear all the elements stored in the Hash Table and empties the Hash table.
Deleting a Particular element
An element can also be removed very easily using the Remove method which takes a Key object as the parameter and remove the element associated with the key.
Syntax
{HashTable object}.Remove(Key as object)
Example, (Using the Hash Table)In this example I am going to store the Employee information into a collection.
Dim ht as new HashTable()
ht.Add("Eno",10)
ht.Add("Ename","Sadha sivam")
ht.Add("EAge",20)
Now to access the Employee name we can use the
item property as follows.ht.Item("Ename")
No comments:
Post a Comment