Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Acceptable types to use as keys in a HashTable

I must admit to having only a rudimentary understanding of how HashTables work, although from what little I do know it seems fairly straightforward. My question is just this: it seems that the conventional wisdom is to use simple, basic value types such as integers for the keys in a HashTable. However, strings are also often used, even though in many languages they are implemented as reference types. What I feel is generally not encouraged is using complex reference types; I'm guessing this is because doing so would necessitate a slower hash function? But then why are strings so commonly used? After all, isn't a string internally a char[] array (again, in most languages)?

In the end, what value types are generally regarded as the "best" (or even simply "acceptable") choices to use as keys in a HashTable? And are there any commonly used choices that are actually regarded as "bad" (like strings, possibly)?

like image 684
Dan Tao Avatar asked Nov 03 '09 20:11

Dan Tao


People also ask

Do Hashtable keys have to be unique?

You cant do that, All the keys in the Hash map or Hash Table should be unique.

Do keys in a Hashtable have to be sortable?

Before moving forward, we need to understand that it is not possible to sort a Hashtable since the data is stored by the hashcode of the key, not by the index . So to sort the data of a Hashtable, we need to have a sortable object like an array or an ArrayList. Sorting has to be done on Key or Value.

Is duplicate key allowed in Hashtable?

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 a key and a value be the same in a hash table?

No, keys must be unique.


1 Answers

It's not a matter of strings versus integers, or value versus reference, but of mutable keys versus immutable keys. As long as the keys are immutable (and thus their hashing value never change) they are OK to index a hash table. For instance, strings in Java are immutable and thus perfectly suited as hashtable keys.

By the way, if a data type is simpe enough to be always passed by value (like scalars), then it will of course be OK.

But now imagine that you use a mutable type ; if you give me a reference to one of these objects as a key, I will compute its hash value and then put it in one of my hashtable buckets. But when you later modify the object, I will have no way to be notified ; and the object may now reside in the wrong bucket (if its hash value is different).

Hope this helps.

like image 196
Gyom Avatar answered Sep 21 '22 15:09

Gyom