Advertisment

Data Structures in C#: Part 3

author-image
CIOL Bureau
Updated On
New Update

In continuation with my last 2 articles on Data Structures part 1 & part 2; I would throw some light on HashTable type of Data Structure.



HashTable

Advertisment



  • It can be used to store multiple items of type Object, along with an associated unique string key.


  • Ideal for situations where large amount of relatively static data will be repeatedly searched for arbitrary keys.


  • Items in a HashTable are sorted in an order determined by a hash code derived from their key.


  • Each object's key value in the collection must be unique, its hash code need not be.


  • Value of HashTable can be NULL.



  • The powerful System.Collections.HashTable collection implements both IDictionary and ICollection .


  • From a performance standpoint, HashTables can retrieve an arbitrary element from the collection very quickly because key searches are limited only to keys with the same hash code, which reduces the number of keys that must be checked to find a match. But, since hash code must be generated for each object and key pair that's inserted into the collection, item insertions are expensive.

Adding Objects to a HashTable





 



 



Retrieving Objects from HashTable



Since HashTable is not a FIFO i.e. First In First Out, objects wouldn't come in the same order as it might have added.



We would implement IDictionary object to retrieve as shown below



 





 



Checking Objects in HashTable



Before we retrieve objects from HashTable we can search the Key and the Value lists in a HashTable for a specific entry.



To check whether Key Exists in HashTable





To check whether Value Exists in HashTable





Happy Hashing! 



tech-news