Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Hashtable to store only keys? [duplicate]

Possible Duplicate:
Which collection for storing unique strings?

I am currently using a Dictionary<string, bool> to store a list of unique identifiers. These identifiers do not need to have any data associated with them - I am just using the Dictionary to be able to quickly check for duplicates.

Since I only need keys, and no values, is a Dictionary the way to go here, or is there another collection I don't know about that would be better suited?

like image 592
We Are All Monica Avatar asked Sep 04 '09 12:09

We Are All Monica


People also ask

Can Hashtable store duplicate keys?

You can't add duplicate keys to hashtables, because hashtables by design can only contain unique keys. If you need to store duplicate key/value pairs, use arrays.

Can Hashtable store duplicate values?

A HashMap or a HashSet isn't a Set, or a Collection either, but that doesn't mean it accepts duplicate keys. you can use Hashset nad for that u have to override hashcode() and equals() method. Actually a Map (whether it is a HashTable, HashMap or whatever) can have duplicate values, but not duplicate keys.

Does every key in a hash table need to be unique?

No. Since the number of possible keys is much greater than the size of the hash table, there must be two keys that map to the same index. Any set that contains those two keys will have a collision.

Can one key have multiple values in Hashtable?

Each key can only have one value.


1 Answers

HashSet<T>

like image 179
Lee Avatar answered Sep 24 '22 20:09

Lee