How does the hash part work in hash maps?

So this is a good picture in the hash maps of the Wikipedia article:

Phonebook hashmap

Everything is still available, except for the hash function in the middle.

  • How can a function generate the right index from any row? Are indices integer? If so, how can a function output 1for John Smith, 2for Lisa Smith, etc.?
+3
source share
5 answers

This is one of the key issues with hashmaps / dictionaries, etc. You should choose a good hash function. A very bad but fast hash function may be the length of the keys. You immediately see that you will get many collisions (different keys, but the same hash). Another bad hash function might be the ASCII value of the first character of your key. Lots of collisions too.
Therefore, you need a function that is much better than these two. You can add (xor) all ASCII values ​​of key characters and mix length, for example. In practice, you often depend on the values ​​(fields) of the object that you want to use for the hash (the same values ​​give the same value type hash =>). For reference types, you can, for example, navigate in a memory location.

. - .

,

+4

- :

$hash = $string[0] % HASH_TABLE_SIZE;

0 HASH_TABLE_SIZE - 1, . -.

- , , .

+1

- ( ) ( -). . , - , . -, - - .

, . (J, o, h, n...), ( ). ASCII UTF , . , , (). , , modulo .

, , , "ab" "ba" . - , - , - .

0

, - ( / ) MSDN:

2: , Hashtable

, .NET( , .NET ), .

0

, -, , . -, "1", .

-1

Source: https://habr.com/ru/post/1741082/


All Articles