A hash table, also known as a hash table , is a data structure that accesses the memory storage location directly from the keyword, which is an unordered list of symbols implemented with an array. The key is stored as the index of the array and the key I in the array is its corresponding value. This allows for fast access to the value of any key. A hash table is a classic example of how the algorithm makes tradeoffs between time and space.
The lookup algorithm for a hash table is divided into two steps :
1. Using a hash function to convert a key to an array index, multiple key hashes may appear above the same index value, which is the second step.
2. Handle Collision collisions (zipper method and Linear detection method).
The concept of hash function
Strictly speaking, each type of key should have a corresponding hash function,
positive integers : In general, the remainder method is used to select an array of prime numbers m, and for any positive integer k, K is divided by M. can be effectively scattered between the 0-m-1. If M is not a prime number, it may not spread evenly.
floating-point number: in Java, the key is represented as a binary and then the save-to-rest method is used.
string : Charat () in Java returns a non-negative 16-bit integer, so long as r is small enough to cause overflow, the result will fall between 0 and M-1
1 int hash = 0; 2 for (int i = 0; i < s.length (); i++) 3 hash = (R * hash + s.charat (i))% M;
View Code
Soft cache
If the hash value is computationally time-consuming, we can cache the hash value of each key, which is the return value of Hascode () for each key that uses a hash variable to hold it.
A hash table based on zipper method
A hash table based on linear detection method
Introduction to the hash table