Overview of Hash table. Overview of Hash table.
Hashtable are very sophisticated data structures that allows accessing an element based on some key, which can be of any type.
1. Is member of System.Collections namespace.
2. Hashtable _table = new Hashtable() ;
Hashtable _table2 = new Hashtable(10); // capacity or size.
Hashtable _table3 = new Hashtable(10 ,06); //capacity , loadfactor
Loadfactor this factor helps for performance, The smaller the load size the more efficiently hash table works and the more memory it occupy.
1. Create a Hashtable
Hashtable _simpleTable = new Hashtable();
2. Added some data in it.
_simpleTable.Add("Key1", "Value1");
_simpleTable.Add("Key2", "Value2");
3. Get the size.
_simpleTable.Count; // an int value.
4. Fetching data
a. create an object of IDictonaryEnumerator , this is an interface that creates an enumerator and customized for Dictonary objects.
IDictionaryEnumerator _enumerator = _simpleTable.GetEnumerator();
b. while (_enumerator.MoveNext())
{
_string += _enumerator.Key + " ";
_string += _enumerator.Value + "\n";
}
c. and that all you fetched the Data !!!
d. clear hash table..
_simpleTable.Clear();
e. Search for a specific key : remember its value type when used
if (_simpleTable.ContainsKey("Key1"))
{
Console.WriteLine("Key1 is present");
}
f. Search for a specific key : remember its value type when used
if (_simpleTable.ContainsValue("Value1"))
{
Console.WriteLine("Value1 is present");
} |