Collection Classes in .net - the HashTable
This is the fourth part of an article on collection classes in .net with C#. This part covers the HashTable.
For the first part see http://www.audacs.co.uk/ViewPage.aspx?PageID=512
For the next part on the Dictionary & SortedDictionary see http://www.audacs.co.uk/ViewPage.aspx?PageID=516
Ok so a hashtable is a collection class of course and it is found at System.Collections.Hashtable
Each item within a HasTable is actually a struct of the type DictionaryEntry. A DictionaryEntry struct contains a key and a value. Thats what HashTables are all about - a collection of things which have a key and a value - eg ISBN Number + Book Title, Employee Number + Name etc. The key and value are actually of type object so you can have rich oo objects within your HashTable although realistically you would probably have a simple type as your key. Each key must be unique though.
So lets have a look at a simple program to work with a HashTable.
I have demonstrated two methods of iterating over the items in a HashTable to give you different options. In essense though a HashTable is really is that simple. The beauty of HashTables though lies in what happens under the hood and why they are called a HashTable.
Now I am not going to into much detail at all in explaining how HashTables work - that's the good thing about encapsulation - we can code something very complicated and wrap it up in a class and then just use that class and trust that it is doing all the work for us under the hood. This is true of Hashtable.
However if you are interested in the underlying science and maths then check out http://en.wikipedia.org/wiki/Hashtable
Hashtables are very good at searching and good at working with large amounts of data. Imagine we had a collection full of objects each with a key - say National Insurance number for the key and then an instance of an Employee class for the value. In this case then items would exist in the collection in the order they were created and so searching for a particular National insurance number could not use any kind of binary chop and so would just have to trawl through the collection one item at a time until it found the one it needed.
Well what I beleive the HashTable does is that it pushes the key of each item throuhg a hash method. This method returns a unique number and so when we search for something then the HashTable will push the search key throuh the hash method producing a number and then the item can be found easily as the hash table is stored sorted by this hashed value and so standard binary chop methods can be used.
This is why we often see HashTables have stored things in what looks like apparently random order.
We can emphasise this last point by running the following code:
So this time we have used a string as the key and as the value. When we run this program though we get the following output with the apparently random sorting!
Thats about it for the HashTable.
For the next part on the Dictionary & SortedDictionary see http://www.audacs.co.uk/ViewPage.aspx?PageID=516