Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of a hash table?

Tags:

I don't have experience with hash tables outside of arrays/dictionaries in dynamic languages, so I recently found out that internally they're implemented by making a hash of the key and using that to store the value. What I don't understand is why aren't the values stored with the key (string, number, whatever) as the, well, key, instead of making a hash of it and storing that.

like image 382
Javier Avatar asked Feb 01 '10 20:02

Javier


People also ask

Why wouldn't you use a hash table?

There are some operations which are not efficiently supported by hash tables, such as iterating over all the elements whose keys are within a certain range, finding the element with the largest key or smallest key, and so on. The O(n) complexity is on average.

Why is a hash table better than list?

A hash table is implemented using a hashing function. It improves data access times. Rather than sorting the data according to a key, it computes the location of the data from the key. In simple terms, it computes an index value from the key of the desired record.


1 Answers

This is a near duplicate: Why do we use a hashcode in a hashtable instead of an index?

Long story short, you can check if a key is already stored VERY quickly, and equally rapidly store a new mapping. Otherwise you'd have to keep a sorted list of keys, which is much slower to store and retrieve mappings from.

like image 135
BobMcGee Avatar answered Oct 05 '22 08:10

BobMcGee