HashMap of hash conflict resolution

Hash Function
  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, depending on the search efficiency and given value is the number of comparisons.
It features hash table: Keyword exists a definite relationship between the table and position it.
  Hash functions: Under normal circumstances, it needs to be established between the keywords stored position in the table with a function 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 space hash value 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: that is, as an address to store the current key value key value that is based on the results of key after a function f (key) to get to (this is the way to keep the value of hashmap), but it has been found that the address on the first count out coming. That place was robbed of it. This is called a hash conflict friends.
Hash function approach to conflict
  1. Open addressing methods:
    

    Wherein m is the length of the table
    incremental di three emulated:
    linear probing re-hash di = 1, 2, 3, ..., m-1
    square the probe re-hash di = 1 2, -2, 4 , - 4, 8, -8, ..., k, square, -k squared
    random hash di probe is then a set of pseudo-random number sequence
            

  2. Method link address
   basic idea of this method is to form a single linked list of all addresses hash synonym chain is called the element i, a single list head pointer and the i-th unit is present in the hash table, thereby find, insert and delete mainly in the synonym chain. Chain address law applicable to the case of frequent insertions and deletions.
  

  

 

 

  

  3. hash then
    this structure is simultaneously a plurality of different hash functions:
  Hi = RH1 (key) I = 1,2, ..., K
  when the hash address Hi = RH1 (key) conflict recalculation hi = RH2 (key) ......, until the conflict is no longer produced. This method is less likely to occur aggregation, but increase computation time.
  4. Establish common overflow area
basic idea of this method are: the hash table is divided into two parts, the base table and overflow table, any table of elements and basic conflict, always populated overflow table
Hash conflict HashMap's approach
  hashmap appear Hash collision when the second option: chain address method.
Code Example:
  there is a "country" (Country) class, we will use the Country object as a key, the name of its capital (String type) as the value. The following example will help us understand the key-value pairs in the HashMap is how the store.

public class Country {
String name;
long population;
public Country(String name, long population) {
super();
this.name = name;
this.population = population;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
// If length of name in country object is even then return 31(any random
// number) and if odd then return 95(any random number).
// This is not a good practice to generate hashcode as below method but I am
// doing so to give better and easy understanding of hashmap.
@Override
public int hashCode() {
if (this.name.length() % 2 == 0)
return 31;
else
return 95;
}
@Override
public boolean equals(Object obj) {
Country other = (Country) obj;
if (name.equalsIgnoreCase((other.name)))
return true;
return false;
}

}


public class HashMapStructure {

public static void main(String[] args) {
Country india = new Country("India", 1000);
Country japan = new Country("Japan", 10000);
Country france = new Country("France", 2000);
Country russia = new Country("Russia", 20000);

HashMap<Country, String> countryCapitalMap = new HashMap<Country, String>();
countryCapitalMap.put(india, "Delhi");
countryCapitalMap.put(japan, "Tokyo");
countryCapitalMap.put(france, "Paris");
countryCapitalMap.put(russia, "Moscow");

Iterator<Country> countryCapitalIter = countryCapitalMap.keySet().iterator();// put debug point at this line
while (countryCapitalIter.hasNext()) {
Country countryObj = countryCapitalIter.next();
String capital = countryCapitalMap.get(countryObj);
System.out.println(countryObj.getName() + "----" + capital);
}
}
}

 

  Join debug in the comment, the structure can be viewed by countryCapitalMap watch:
          

 

 


It can be seen from the figure to the following:

there is a table called the Entry size of the array 16.
The Entry table array memory object class. HashMap class has an inner class called Entry. This class contains the Entry key-value as instance variables. Entry class structure that we look at. Entry structure classes: 
static class Entry {the implements of Map.Entry
Final Key K;
V value;
Entry Next;
Final int the hash;
... // code goes here Wallpaper More
}

    1) whenever the time to hashmap stored inside the key-value pairs, as they will instantiate an Entry object, the object will be stored in the Entry Entry Array in the aforementioned table. Now you must be wondering, Entry objects created above which will be stored in a specific location (in the precise position in the table). The answer is, according to the key hashcode () method calculated hash value (to decide). used to calculate the hash value in the key index Entry array.
    2). Now, if you look at the array index figure above 15, it has a HashMap $ Entry Entry object is called.
    3) We put 4 to hashmap key-value pairs, but it looks like only one element! ! ! This is because, if the two elements have the same hashcode, they will be placed on the same index. The question arises, how to put it? It is the original (logical) in the form of a linked list (the LinkedList) to store. Thus they are stored hash value of the position on the 15, and a plurality of Entry, link with next.


======================================================================

HashMap underlying solutions, and implementation of hash conflict

We usually have used hashMap, but we may realize the underlying hashMap do not quite understand, while the hash conflicts which may arise some do not understand, these days I turned the next information, but also to understand a little lower, recorded, so as not to forget .

The figure is a hash table (Hash table, also called a hash table), based on the key code 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. But when a relatively large number of keywords when, inevitably will cause a problem, is not the same keywords hidden incident on the same address, thus causing a problem that hash conflict. So how do you solve it?

Commonly used methods are generally more open address method:
1. Open addressing is: Hi = (H (key) + di) MOD m, i = 1,2, ..., k (k <= m-1), wherein H (key) to the hash function, m is the length of the hash table, increments DI sequence, the following three can be emulated:
1.1 DI = l, 2,3, ..., m-. 1, said re-hash linear probe;. order to see the next cell of the table until you find an empty unit, or search through the entire table.
1.2. Di = 1 ^ 2, -1 ^ 2,2 ^ 2, -2 ^ 2, ⑶ ^ 2, ..., ± (k) ^ 2, (k <= m / 2) detecting said secondary re-hash ; jump sounding around the table.
1.3. Di = pseudo random number sequence, and then detecting said pseudo-random hash. Detect a random number generated.

2 re-hashing: establishing a plurality of hash functions, if the time when the hash conflict, use the next hash function, can be stored until you find the location of elements.

Method 3 fastener (link address method): CV is a conflict of position on the list, then the element is inserted into the list of the trailing end of the conflict,

4 establishing a common overflow area: the hash table into the base table and overflow table, the elements of conflict with the basic table placed in the overflow table.

HashMap underlying list is an array and implemented, which is above the fastener method. First, when inserted, will be then calculated based on the hash value of the key corresponding to the array index was calculated as the index = hashcode% table.length, (the subscript is the above-mentioned bucket), when the index has been above when present it will form a list of elements, the elements inserted into the trailing end, if no elements are present above the standard, then the element directly into this position.
When the query, the same will first calculate the corresponding index based on the hash key, and then to to find the corresponding position, if there are many elements of this index above, then will have to find until you find correspondence on this list Elements.

Guess you like

Origin www.cnblogs.com/weigy/p/12571648.html