understanding Hashtable

enzom83

Member
Joined
Nov 28, 2005
Messages
18
Programming Experience
5-10
Hi,
It is possible avoiding collisions within an Hashtable object if I know the max number of the entries which will be inserted into?

Thanks!
 
I don't really understand the question. Every key in a HashTable must be unique but the values have no such constraint, so they may be unique or not. If that doesn't answer your question can you please rephrase it a bit more clearly? What does knowing the maximum number of entries have to do with anything?
 
A Hashtable implements the IDictionary interface and as such it is a data structure that supports key-and-value pair look ups. The Hashtable is defined by two variables: The load factor and the initial capacity. The initial capacity is the number of buckets in the hashtable at the time of creation. The load factor is the ratio of the maximum ratio of elements to buckets. Each time an element is added to a hashtable, the element is placed in a bucket based on the hash value of the key. When a value is retrieved from a hashtable, the correct bucket location is determined by taking the hash value of the key and the bucket elements are searched sequentially for the correct element.
By increasing the load factor, we achieve better memory utilization, but cause the program to be slowed by increased hashing collisions. By decreasing the load factor, we achieve better program speed due to a reduction in hashing collisions, but we get poorer memory utilization, because a larger portion of the hash table remains empty.

Therefore, if I want to add only 30 elements to an Hashtable and I create a new Hashtable by specifying a capacity of 30 elements, I could be sure in avoiding "collision"?

Thanks!
 
Back
Top