Common data structures hash table

Common data structures hash table

To know what a hash table, you must first understand hash function

Hash function

Address index = H (key)
Plainly, hash function is to be calculated according to the memory address key position, and the hash table is a hash function based on a look-up table to establish

Several common hash function (hash function) constructor

  Direct-addressable

    • A linear function takes a hash value of the keyword or keyword address.
    • I.e. H (key) = key or H (key) = a * key + b, where a and b are constants.

    Write pictures described here

  In addition stay 

    • Take a keyword is not greater than the length m of the hash remainder number p, obtained as the hash address.
    • 即 H(key) = key % p, p < m。 

    Write pictures described here

  Digital analysis 

    • When the digits to the keyword is greater than the address of the keywords you distribution analysis, select any of several evenly distributed as the hash address.
    • It applies only if all the keywords are known, according to part of the actual application you sure you want to select, try to avoid conflict.

     Write pictures described here

  Square reindeer method 

    • First calculate the key value of the square, then squared several intermediate value as the hash address.
    • Keyword randomly distributed, the resulting hash address is randomly distributed.

     Write pictures described here

  Folding method (superposition) 

    • The number of bits the same keywords into several parts, and then take these fractions are superimposed and (rounding carry) as the hash address.
    • Keywords for more digits, and keywords on every digital distribution is substantially uniform. 

     Write pictures described here

    Random number method

    • Select a random function, the random function key value as its hash value.
    • When this method is usually unequal length of a keyword. 

Method of constructing a hash function of a lot of practical work to select the appropriate method depending on the circumstances, the general principle is as little conflict.

Factors to consider are usually the length and distribution of keywords, such as the range of hash values.

Such as: When the key is an integer type when can I stay with addition; if the keyword is decimal type, choose a random number method will be better.

 

Before JDK8, using the underlying list array + implemented, it can be a list of array elements

After JDK8, the length of a long time, the underlying implementation of the optimization

 

What is a hash table

 Hash table (Hash table, also called hash table), is the basis of the key value (Key value) to directly access a data structure. In other words, to access the records by key values are mapped to table a position to speed up the search. This mapping function called a hash function , recording storage array is called a hash table .

Recording the storage location = f (key)

Where f is called the correspondence between the hash function, also called a hash (Hash function), using the hashing technique records are stored in a contiguous storage space, this storage space continuously or hash table called a hash table ( Hash table).

 

 

 

 

 

 

 

 

 

<Hash functions and addressing>

    Ideally hash sufficiently large, the data stored in each memory cell within a hash, so look for inserting and deleting certain data can be obtained directly. But in reality hash table can not be infinite, and the number of stored data is theoretically no limit to the number of such stored data is far greater than the number of memory cells hash table.

    In order to achieve O (1) within the data insertion and deletion and search, data must be mapped into a fixed position in the hash table, the mapping function is a hash function. Hash functions computed by a data address location in the hash table.

 

 

 

 

Hash table using O (1) time of data insertion and deletion and search, but does not guarantee the order of the hash table data in the table, this hash table lookup in most large data or the data is a minimum time O (N).

HashSet (); Constructs a new, empty; I think BACKGROUND ashMap instance has an initial capacity (16) and the load factor (o.75).

 

 

Guess you like

Origin www.cnblogs.com/xiaozhongfeixiang/p/11571902.html