Monday, March 12, 2012

what is the difference between HashTable and SortedList?

hi there

I just learn HashTable and SortedList from some answers of questions I posted here

both of them seems are very useful when we need to count some values

I used to use array and run a loop to count the value but if use HashTable or SortedList , things become easy

I only know the difference between HashTable and SortedList is , SortList sort items automatically

just wondering if they have more difference?

if we don't need the sort function, which one is better to use?

thank you

Hi,

As far as I know, SortedList is a binary search. And in a Hashtable, the keys are not search sequentially. The running time of a binary search is in proportion to log2(N), which is decently good. But still theHashtable performs better since the running time is constant. So the HashTable peforms is better in searching, and the SortedList is better in sorting. If you don't need the sort function, please use HashTable I think.

You should read more about thehashtable to get an understanding of how it works, see:

http://msdn2.microsoft.com/en-us/library/system.collections.hashtable.aspx

I hope this helps


hi,

have a look on this page, it may help you

http://www.cookcomputing.com/blog/archives/000092.html


hashtable stores values in key/value pair

sorted list is acombination of hashtable and arraylist, also it allows sorting of elements whereas hashtable doesn't....


HashTable allows you to store the value against a key which must be unique. like employee object against a EmployeeId

But Sorted list is bit different. This is like array list only difference is that this automatically sort the data you store in sorted list.


thanks all ,

I have a question about this → HashTable allows you to store the value against a key which must be unique. like employee object against a EmployeeId

does that mean I can have same key in SortedList

(ex: SortedList[0] = 1 , SortedList[1] =1 , both same key =1 is OK) but not OK in HashTable)

(ex HashTable[0] = 1, HashTable[1] =1, 1 is already exist in HashTable[0] so HashTable[1] can't be 1 as well?)

0 comments:

Post a Comment