Hashtable
- It’s a data structure which is used to store a collection of key/value
items, each object has a HashCode which uniquely identifies that object and
based on it the object is places in a bucket from where its retrieved as and
when needed.
- To generate Hash-Code for an object a Hashing algorithm is
used, it’s the efficiency of Hashing
algorithm which decides performance of Hashtable. A good hashing algorithm will always generate
a unique and same hashcode for any object.
- Each object in hashtable is stored in a bucket, and a bucket corresponds
to a hashcode. Based on number of items
stored on bucket, load-factor for hashtable is collected. Loadfactor
is defined as a ration of number of items present in bucket to its size. Best value of Loadfactor is 1, but it can be
any number between 0 to 1 based on memory available and requirement.
- As elements are added to a Hashtable, the actual load factor of
the Hashtable increases. When the actual load factor reaches the specified load
factor, the number of buckets in the Hashtable is automatically increased to
the smallest prime number that is larger than twice the current number of
Hashtable buckets.
- Hashtable in .Net implemens IDictionary<TKey,TValue>
interface.The objects which are used as keys are supposed to implement “IEqualityComparer”
or “IHashProvider” & “IComparer”. IEqualityComparer is nothing but it’s a union
of IHasProvider and IEqualityComparer.
- Based on your requirement you can avoid implementing IHashProvider
for Key Object by implementing it separately and then passing that
implementation in Hashtable constructor.
- IHashProvider has method GetHashCode which is used to get Hash-Code
and that Hash-Code is used for deciding a bucket in which the element will be
placed. This condition makes it compulsory for key objects to be immutable, as
if they change then their corresponding Hash-Code will also change.
- IComparer has Equal method which is used for comparing 2 objects.
- Hastable in C# implements IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable.
No comments:
Post a Comment