views:

254

answers:

2
HashSet<T> t = new HashSet<T>();
// add 10 million items


Dictionary<T, K> t = new Dictionary<T, K>();
// add 10 million items.

Whose .Contains method will return quicker?

Just to clarify, my requirement is I have 10 million objects (well, strings really) that I need to check if they exist in the Data Structure. I will NEVER iterate.

+4  A: 

I assume you mean Dictionary<TKey, TValue> in the second case? HashTable is a non-generic class.

You should choose the right collection for the job based on your actual requirements. Do you actually want to map each key to a value? If so, use Dictionary<,>. If you only care about it as a set, use HashSet<>.

Jon Skeet
Yes, I meant Dictionary<TKey, TValue>. I'm only concerned about searching for item's existence in a data structure, that is *all*.
halivingston
@halivingston In that case use HashSet. It makes it obvious that that *is* all you need.
Jon Skeet
Ok, thanks. I actually have a HashSet<TKey> right now, and a duplicate copy of Dictionary<Tkey, TValue> also in memory. I first .Contains on the HashSet, then retrive the value in Dictionary<TKey, TValue>. I have infinite memory right now, but soon I fear my memory will be constrained and our team will ask me to remove this duplicate stuff in memory, at which point I'll be forced to use Dictionary<TKey, TValue>.
halivingston
You do know Dictionary has a ContainsKey function too right? Why are you duplicating data?
Blindy
60% of the time there will be a miss, so I'm thinking I can double my memory cost.Maybe I should reconsider and just use Dictionary.
halivingston
If you already have the data in the dictionary, then your first comment is clearly incorrect - you need to associate keys with values as well. Maybe not for *this* particular bit of code, but that's irrelevant. If you've already got a `Dictionary` for other reasons, you should use that.
Jon Skeet
+1  A: 

These are different data structures. Also there is no generic version of HashTable.

HashSet contains values of type T which HashTable (or Dictionary) contains key-value pairs. So you should choose collection on what data you need to be stored.

Andrew Bezzub