What's the point of a hash table?

后端 未结 11 1460
耶瑟儿~
耶瑟儿~ 2020-12-28 17:01

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

相关标签:
11条回答
  • 2020-12-28 17:18

    Generally the point of a hash table is to store some sparse value -- i.e. there is a large space of keys and a small number of things to store. Think about strings. There are an uncountable number of possible strings. If you are storing the variable names used in a program then there is a relatively small number of those possible strings that you are actually using, even though you don't know in advance what they are.

    0 讨论(0)
  • 2020-12-28 17:18

    Also consider speed. If your key is a string and your values are stored in an array, your hash can access any element in 'near' constant time. Compare that to searching for the string and its value.

    0 讨论(0)
  • 2020-12-28 17:19

    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.

    0 讨论(0)
  • 2020-12-28 17:20

    What I don't understand is why aren't the values stored with the key (string, number, whatever) as the, well, key

    And how do you implement that?

    Computers know only numbers. A hash table is a table, i.e. an array and when we get right down to it, an array can only addressed via an integral nonnegative index. Everything else is trickery. Dynamic languages that let you use string keys – they use trickery.

    And one such trickery, and often the most elegant, is just computing a numerical, reproducible “hash” number of the key and using that as the index.

    (There are other considerations such as compaction of the key range but that’s the foremost issue.)

    0 讨论(0)
  • 2020-12-28 17:20

    A hashtable is used to store a set of values and their keys in a (for some amount of time) constant number of spots. In a simple case, let's say you wanted to save every integer from 0 to 10000 using the hash function of i % 10.

    This would make a hashtable of 1000 blocks (often an array), each having a list 10 elements deep. So if you were to search for 1234, it would immediately know to search in the table entry for 123, then start comparing to find the exact match. Granted, this isn't much better than just using an array of 10000 elements, but it's just to demonstrate.

    Hashtables are very useful for when you don't know exactly how many elements you'll have, but there will be a good number fewer collisions on the hash function than your total number of elements. (Which makes the hash function "hash(x) = 0" very, very bad.) You may have empty spots in your table, but ideally a majority of them will have some data.

    0 讨论(0)
  • 2020-12-28 17:21

    what is hash table?

    It is also known as hash map is a data structure used to implement an associative array.It is a structure that can map keys to values.

    How it works?

    A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.

    See the below diagram it clearly explains.

    enter image description here

    Advantages:

    In a well-dimensioned hash table, the average cost for each lookup is independent of the number of elements stored in the table.

    Many hash table designs also allow arbitrary insertions and deletions of key-value pairs.

    In many situations, hash tables turn out to be more efficient than search trees or any other table lookup structure.

    Disadvantages:

    The hash tables are not effective when the number of entries is very small. (However, in some cases the high cost of computing the hash function can be mitigated by saving the hash value together with the key.)

    Uses:

    They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches and sets.

    0 讨论(0)
提交回复
热议问题