views:

11224

answers:

4

I am trying to figure out when and why to use a Dictionary or a HashTable. I have done a bit of a search on here and have found people talking about the generic advantages of the Dictionary which I totally agree with, which leads the boxing and unboxing advantage for a slight performance gain.

But I have also read the Dictionary will not always return the objects in the order they are inserted, thing it is sorted. Where as a HashTable will. As I understand it this leads to the HashTable being far faster for some situations.

My question is really, what might those situations be? Am I just wrong in my assumptions above? What situations might you use to choose one above the other, (yes the last one is a bit ambiguous).

+32  A: 

System.Collections.Generic.Dictionary<TKey, TValue> and System.Collections.Hashtable classes both maintain a hash table data structure internally. None of them guarantee preserving the order of items.

Leaving boxing/unboxing issues aside, most of the time, they should have very similar performance.

The primary structural difference between them is that Dictionary relies on chaining (maintaining a list of items for each hash table bucket) to resolve collisions whereas Hashtable uses rehashing for collision resolution (when a collision occurs, tries another hash function to map the key to a bucket).

There is little benefit to use Hashtable class if you are targeting for .NET Framework 2.0+. It's effectively rendered obsolete by Dictionary<TKey, TValue>.

Mehrdad Afshari
@Jon- The chaining and rehashing is discussed in depth here- http://msdn.microsoft.com/en-us/library/ms379571(VS.80).aspx
RichardOD
and in detail in *the* algorithm book: Introduction to Algorithms, 2nd ed. by CLRS
Mehrdad Afshari
Thank you both. Just found that page as Richard posted it... Was going to ask about Chaining but the MSDN site is actually helpful!
Jon
@Mehrdad - What's not clear to me about how collisions are resolved is this:if multiple keys could result in the same hash, then how do you ensure you're getting the right value on lookups, ie how does the function know which element to return? In http://msdn.microsoft.com/en-us/library/ms379571%28VS.80%29.aspx it says,"Rather than reprobing in the event of a collision, as is done with the Hashtable class, the Dictionary simply chains any collisions onto the bucket's list." Does this mean that when using Dictionary, collisions are not something the developer has to worry about?
Howiecamp
@Howiecamp: This is not really much different from `Hashtable`. Hash tables store 3 pieces of information in an entry: key hash, key itself, and the value. For items with equal hash, it'll have to traverse the list to find the item with equal key and return its value. This is pretty much true for `Hashtable` too. As a developer using a `Dictionary` normally, you don't need to worry about it.
Mehrdad Afshari
@Mehrdad Just to be clear, both the Hashtable and Dictionary objects store the key itself, and both also hide collisions from the developer?
Howiecamp
Yes. This is how all hash tables work in general.
Mehrdad Afshari
+4  A: 

Both are effectively the same class (you can look at the disassembly). HashTable was created first before .Net had generics. Dictionary, however is a generic class and gives you strong typing benefits. I would never use HashTable since Dictionary costs you nothing to use.

Adam Luter
+2  A: 

Another important difference is that the Hashtable type supports lock-free multiple readers and a single writer at the same time, while Dictionary does not.

Steven
+1  A: 

I guess it doesn't mean anything to you now. But just for reference for people stopping by

Performance Test - SortedList vs. SortedDictionary vs. Dictionary vs. Hashtable

Munim Abdul
interesting, thanks
Jon