1, the principle of hashing
The occurrence of a hash is due to a traditional data structure such as a linear table (array, linked list, etc.) in the tree. There is no corresponding relationship between the keyword and other storage locations. Therefore, when looking for keyword, there is a need to compare each other, despite the two-point search and other improved efficiency of the search algorithm.
But that's not enough. I hope that we will not pass any comparison when querying keyword. Once accessed, the records can be retrieved. Therefore, we must establish the corresponding relationship f between the keyword and its corresponding storage location. Such a corresponding relationship F is called a hash function, and the table created by this thought is a hashtable.
The key is how the hash function is structured.
There are several ways to do this:
1" the direct address method
takes keyword or keyword of some linear function value as a hash.
2) Numerical analysis Method
3) The square takes the middle method
takes keyword square after several is the hash address.
4) The folding method
cuts the keyword into the same number of bits (the last part of the bits can not be used), and then takes the superposition of these parts and (rounding up) as the hash address.
5) The remainder method
takes the remainder as a hash address that is keyword by a number p that is not longer than the hash table length (hash_table_length).
H (key) = key% p ( where p is less than or equal to the hash table length Hash_table_length)
6) The random number method
takes keyword's random function value as its hash address.
After you have identified the hash function, you will have to resolve the problem of the Harlem conflict, often using methods such as the following:
1) Open addressing method
Hi = (H (key) + di)% m i = 1, 2, 3,..., K (k <= M-1)
: H (key) is a hash function. M is a hash table length, di is an incremental sequence, and Di may have the following three ways:
A linear probe re-hash: di = 1, 2, 3, ..., M-1
B Two-time probe hash: di = (+,-) k^2, (k <= m/2)
C random probe re-hash: di is Random number sequence
2) hashing
3) Chain address method
4) Create a public overflow zone
2, how HashMap in Java is implemented
< Span style= "font-size:18px" >hashmap
We can understand it as " array of list ".
HashMap is actually implemented as a linear array, so the container that can be understood to store the data is a linear array.
So how does a linear array implement key-value pairs to access data? Here HashMap has to do some processing.
1. First hashmap inside a static inner class entry is implemented. Its important attributes are key, value, next, from the attribute Key,value we can see very clearly entry is the HashMap key value of the implementation of a basic bean, we said that HashMap is based on a linear array, This array is the contents of the Entry[],map are stored in the entry[] inside.
2, the resolution of hash conflict in HashMap ( Chain Address Method ): the entry class has a Next property that refers to the downward entry.
Whenever the same index has a new node (a) inserted. A becomes the head node for this index. Then a->next= the old head node.
2014 Ali intern face question--the principle of hashing and how HashMap is implemented in Java