Map & collection

Hash function

Can achieve O (1) lookup complexity through a hash table.

 

Hash function construction: two basic principles of the design of a good hash function, calculating a simple distribution +

1. Direct addressing method

  With its own key addressed to a linear function, f (key) = a * key + b. But we need to know the distribution of keywords.

2. Digital Analysis

  Keywords for handling relatively large number of bits. Extracted as part of the key hash address.

3. square reindeer method

  After taking the square key intermediate of several digits as the hash address.

4. Folding Method

  The key is divided into an equal number of bits in several parts, these parts superimposed summation, press the hash-table to take a few long as the hash address.

5. Except I stay

  Most used. Hash table length m, f (key) = key mod p, p is equal to less than m. p the choice is very important, the election may result in poor hash collision.

 

Approach to hash conflict: ! Key1 = key2 but f (key1) == f (key2 ), hash collision.

1. Open-addressable: the event of conflict, went to the next empty hash address, as long as the hash table is large enough, can always find an empty hash address.

  fi (key) = (f (key) + di) mod p, di = 1, 2, ..., p-1 the linear detection method.

  Such direct comparison di blind value, by modifying the values ​​di manner, for example squared, to try to solve the stacking problem, the second detection method.

  fi (key) = (f (key) + at) mod p, = 1, 2 , -1 2 , 2, 2 , -2 2  , ..., q 2 , q 2  , q <= p / 2

  Or directly with a random function value di, called random detection method.

  fi (key) = (f (key) + di) mod p, di sequence generated by a random function.

 

2. re-hash function method, a plurality of hash functions, when a conflict occurs, the next call to a hash function is calculated. Finally the conflict may be low.

 

3. Chain address method (method zipper), commonly used method. Using a single chain, instead of the hash table stored recording elements but address record, if a conflict occurs, then to the chain from the preceding element to the recording conflict.

 

4. The common overflow area method, for continuously recording conflict with overflow table is stored separately, if the hash search is not, go to find the overflow area. For a relatively good hash function, high efficiency of this method of conflict resolution.

 

List : implementation can be an array can also be a linked list, elements can be repeated. Insert O (1), find the O (n).

Map : key-value pair, with the key key will not be repeated.

The SET : does not permit duplicates. A hash table (or binary) to achieve, so look O (1) complexity (or O (logn)).

 

A hash table implementations, disordered arrangement of elements, look O (1); implemented using binary search trees, are arranged relatively orderly elements, look O (logn).

 

 

Each language hashmap:

 

Each language hashset:

 

Guess you like

Origin www.cnblogs.com/chaojunwang-ml/p/11333207.html