Introduction
A Hashtable is a collection that stores (Keys, Values) pairs. Here, the Keys are used to find the storage location and is immutable and cannot have duplicate entries in the Hashtable. The .Net Framework has provided a Hash Table class that contains all the functionality required to implement a hash table without any additional development. The hash table is a general-purpose dictionary collection. Each item within the collection is a DictionaryEntry object with two properties: a key object and a value object. These are known as Key/Value. When items are added to a hash table, a hash code is generated automatically. This code is hidden from the developer. All access to the table's values is achieved using the key object for identification. As the items in the collection are sorted according to the hidden hash code, the items should be considered to be randomly ordered.
The Hashtable Collection
The Base Class libraries offers a Hashtable Class that is defined in the System.Collections namespace, so you don't have to code your own hash tables. It processes each key of the hash that you add every time and then uses the hash code to look up the element very quickly. The capacity of a hash table is the number of elements the hash table can hold. As elements are added to a hash table, the capacity is automatically increased as required through reallocation. It is an older .Net Framework type.
Declaring a Hashtable
The Hashtable class is generally found in the namespace called System.Collections. So to execute any of the examples, we have to add using System.Collections; to the source code.The declaration for the Hashtable is:
Hashtable HT = new Hashtable();
This new Hashtable has the limited capacity and when the limit is reached then the capacity is automatically increased to allow for further storage. Since the nature of the Hashtable dictionary is that the capacity is always considered to be approximate we can set the initial approximate capacity by passing the integer parameteger to the constructor as:
Hashtable HT = new Hashtable(100);
This is useful when the maximum size of the collection is known because it removes the need of resizing the hash table and also it increases the performance.
Properties of Hashtable
Some of the important properties of a hash table are:
Comparer: Gets or Sets the IComparer to use for the Hash Table.
Count: Gets the number of key/value pairs contained in the hash table.
IsReadOnly: Get a value indicating whether the hash table is read-only.
Item: Gets or Sets the value associated with the specified Key.
Keys: Gets an ICollection containing the keys in the hash table.
Values: Gets an ICollection containing the values in the hash table.
Methods of Hashtable
Some of the important methods of a hash table are:
Add: Adds an element with the specified key and value in the hash table.
Clear: Removes all the elements in the hash table.
ContainsKey: Determined whether the hash table contains a specified key or not.
ContainsValue: Determined whether the hash table contains a specified value or not.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; //must
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Hashtable HT = new Hashtable();
HT.Add(1,"s");
HT.Add(3, "n");
HT.Add(4, "j");
HT.Add(2, "a");
HT.Add(5, "u");
foreach (object i in HT.Keys)
Console.WriteLine(i);
foreach (object J in HT.Values)
Console.WriteLine(J);
foreach (DictionaryEntry di in HT)
Console.WriteLine("keys={0} values={1}", di.Key, di.Value);
Console.ReadKey();
}
}
}
This program will display all the DictionaryEntry objects returned from the enumerator in the foreach loop. The WriteLine call contains a format string that displays the key/value pairs with a comma. The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection. Finally, the output will be:
Note:
A hash table does not maintain an ordered collection; there is no specific order to the collection of keys or values obtained. Each element is a key/value pair stored in a DictionaryEntry object. A key cannot be a nullptr, but a value can be.
Advantages of Hashtable
It allows the execution time of look up, retrieve, and set operations to remain nearly constant, even for large sets.
It has the ability to locate the items qickly even when the amount of data is huge.
No requirement to scan through the entire data set to find an item or to sort the set in order to perform a Binary search.
The key can be hashed and the value's location found directly, if the key for a desired data value is known.