Hashtable question
-
Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
-
Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
You don't. Hashtables do not store the sequence that you added the items. You should use an arraylist.
-
You don't. Hashtables do not store the sequence that you added the items. You should use an arraylist.
Indeed. If you want your data to be always sorted, ArrayList is probably better than Hashtable. If you use Hashtable because most of the times you don't care about sorted data, but you'd rather have a near-O(1) access time, then Hashtable is better. Note, however, that you can use an Hashtable and, when needed, sort the values as needed and live happily ever-after. Frank
-
Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
You might consider the
System.Collections.Specialized.ListDictionary
. It implements the IDictionary interface with a linked list. It would be my guess (just a guess) that the order of addition is maintained. Be aware, according to the documentation, ListDictionary is superior to hashtable performance-wise up to about ten items. If your list is longer, consider another strategy.α.γεεκ
Fortune passes everywhere.
Duke Leto Atreides -
Hey i am using a hashtable, and i am adding my fields collection to it. when i iterate through them they found to be not in the order in which i added them. how do i maintain the sequence in which the elements are added. thanks Cheers, Venkatraman Kalyanam Bangalore - India "Being Excellent is not a skill, it is an attitude" Reality is an illusion caused by caffeine deficiency(one Microsoft Research scholor)
For me, the best solution tends to be maintaining two data structures, an array or arraylist with the keys in the desired order, and a hashtable with the key-value pairs. You could easily devise data structures that combine the two, such as a hashtable where the list of keys is a linked list.