Hash table (a) of the four approaches to resolve conflict

A) Introduction to hash table

Features Non hash table: key position in the table and it does not exist relationship between a determined, the process of looking for and once each keyword compares a given value, efficiency and look for depends on the given value Compare the number of times.

    It features hash table: Keyword exists a definite relationship between the table and position it.

Hash functions: In general, you need to key it in the table and stored to establish a functional relationship between the position to f (key) as a key for the key position of the record in the table, usually called the function f (key) is a hash function.

hash: translated as "hash", is the input of any length, through a hash algorithm, a fixed length into an output , the output is the hash value.

           This conversion is a compression map , the hash value of the space is typically much smaller than the input space , different inputs may hash to the same output, it is impossible to uniquely determine the value of the input from the hash value.

           Simply means that the message of any length A to the compression function of the message digest Moi fixed length.

hash conflict: (Big Brother himself wrote oh) is based on the results through a function key that is f (key) to get the address stored as the current key value key-value pair (this is the way to keep the value of hashmap), but found it has been the first to the last address counted out. That is the place you want to squeeze it. This is called a hash conflict friends

B) the method of dealing with conflict hash function

1) Open addressing method:

Wherein m is the length of the table

There are three incremental borrowing di:

Linear probing re-hash di = 1, 2, 3, ..., m-1

Square probe re-hash di = 1 2, -12, 22, -22, 32, -32, ..., k2, -k2

( Big Brother Remark : single right, above the square of the probe re-hash is squared plus 1; 1 minus the square, the square plus 2, minus 2 squared, the squared plus 3, plus minus the square of k ... 3 square squared minus k. rub lying, teacher could you pit it? law School. If you look at the square directly detect re-hash of di is how come, may not be able to read the writing teacher ppt, is the the square of the mean. above it in red, the equivalent of the teacher's ppt, corresponding picture above to see together.)

Di random hash probe is then a set of pseudo-random number sequence

example:


I wrote that red above the bottom of the figure with 12 , I was tested when I do not know the 12, which is above the incremental di origin. I do not know, restrict know, it was the power of 2 1. . . . The teacher would say too lazy or playing digital subscript.


2) Method link address

This is just above the teacher's ppt, following personally put the entire test.


According to the hash algorithm ppt: h (key) = key % 7, corresponding to the calculated hash value, the hash value of the temporary decision, the current value, the position is stored in the array.
After completion are considered, it can be, according to this hash value, in turn, these numbers are placed below the array. Then there is my own this screenshot.
And above ppt projection is consistent.

, The simple explanation of this approach is Java HashMap is so implemented, the source of this list HashMap generation mechanism.
Inside put () method, there follows the last part of the call.
addEntry (hash, key, value, i);
explain the meaning of several parameters:
. 1, the hash: key is based on a calculated value, and so the source is dropwise -int hash = hash (key) ;,
the counted out this is equivalent to the identity card number, can uniquely identify a person, uniquely determine the map
2, key: key is that when we go hashmap which put the key to the key, use the map when you can not get the key according to value it.
3, value: the above matter, the value is stored in the key-value pairs.
4, i: source so there is dropwise -int i = indexFor (hash, table.length ); this means that the actual value pairs stored in the index of the underlying array subscripts.
Then the i, that value may correspond to the modulo after ppt, which is determined on the array subscripts.

Although put in the time, the issue of expansion may occur, but that we do not consider this, only consider how to generate the list, and the order of the keys on the list.
createEntry (hash, key, value, bucketIndex);
This method is truly creating a node to the array.
These parameters are the same, and the same meaning as explained above.

  1. // remove the original start value on the array, a new node to stuffed, and the new node and then placed on an array.  
  2. // That is the truth from behind. ppt painted on it a little bit wrong.  
  3. // teachers Well, that I enjoyed, generally do not care about this stuff.  
  4.    void createEntry(int hash, K key, V value, int bucketIndex) {  
  5.        Entry<K,V> e = table[bucketIndex];  
  6.        table[bucketIndex] = new Entry<>(hash, key, value, e);  
  7.        size++;  
  8.    }  
   //先从数组上取下原来的值,给塞到新的节点去,然后把新的节点再放到数组上。
    //也就是后来居上的道理。ppt上画的也就有点毛病了。
    //老师们嘛,就是 混口饭吃,一般都不斤斤计较这东西的。
    void createEntry(int hash, K key, V value, int bucketIndex) {
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<>(hash, key, value, e);
        size++;
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  1.   static class Entry<K,V> implements Map.Entry<K,V> {  
  2.       final K key;  
  3.       V value;  
  4.       Entry<K,V> next;  
  5.       int hash;  
  6.       /** 
  7.        * Creates new entry. 
  8.        */  
  9.       Entry(int h, K k, V v, Entry<K,V> n) {  
  10.           value = v;  
  11.           next = n;  
  12.           key = k;  
  13.           hash = h;  
  14.       }  
  15. //******  
    static class Entry<K,V> implements Map.Entry<K,V> {
        final K key;
        V value;
        Entry<K,V> next;
        int hash;
        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) {
            value = v;
            next = n;
            key = k;
            hash = h;
        }
        //******
    }
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

The above model is stored on hashmap underlying array element. The key chain can also form, we are interested can look at hashmap 1.7 source code.

3, 4) re-hash, the establishment of a common overflow area

3. The method of re-hash, is more than a calculated hashcode method, a calculated and if it is repeated, then the other algorithm to count. Anyway, a lot, is not repeated until slightly up. Big Brother guess

4. Establish a common overflow area, it is the conflict are in another place, not in the table inside. Realization do not know, Big Brother is a guess.

Summarize the following four lines is the word:
1. Open-addressable (linear probing re-hash, then the hash of the secondary probe, and then detecting a pseudo-random hash)
2. then hashing
3. Chain address method (Java hashmap is to do so)
4. establish a common overflow area


See this, still have to stop and look at myself hashmap source code, easy to understand 1.7, I also made a comment, you can take a look at the following link

Java 

Good understanding of the connection doll of 1.8 hashmap

Java hashmap understanding 1.7, more than a red-black tree or something.



Published 94 original articles · won praise 55 · views 110 000 +

Guess you like

Origin blog.csdn.net/Suubyy/article/details/100120892