The solution and principle analysis of hashmap conflict

Source: Internet
Author: User

In the Java programming language, the most basic structure is two kinds, one is an array, one is the analog pointer (reference), all the data structure can use these two basic structure constructs, HashMap also. When the program attempts to put multiple key-value into the HASHMAP, take the following snippet as an example: Hashmap<string,object> m=new hashmap<string,object> ();
M.put ("A", "rrr1");
M.put ("B", "TT9");
M.put ("C", "TT8");
M.put ("D", "G7");
M.put ("E", "D6");
M.put ("F", "D4");
M.put ("G", "D4");
M.put ("H", "D3");
M.put ("I", "D2");
M.put ("J", "D1");
M.put ("K", "1");
M.put ("O", "2");
M.put ("P", "3");
M.put ("Q", "4");
M.put ("R", "5");
M.put ("S", "6");
M.put ("T", "7");
M.put ("U", "8");
M.put ("V", "9");

        hashmap uses a so-called "Hash algorithm" to determine where each element is stored. When the program executes the Map.put (String,obect) method, the system will call the String's Hashcode () method to get its hashcode value-each Java object has a hashcode () method, which can be obtained by the method's Hashcode Value. After the hashcode value of the object is obtained, the system will determine the storage location of the element based on the hashcode value. The source code is as follows:    public v put (k key, v value)  {            if  (key == null)                 return putfornullkey (value);            int hash = hash (Key.hashcode ());            int i = indexfor (hash, table.length);            for  (entry<k,v> e = table[i]; e  != null; e = e.next)  {               object k;                //determines whether an element of the same hashcode and the same key exists at the currently determined index position, and if there are elements of the same hashcode and the same key, the new value overwrites the original old value and returns the old value.                //If there is the same hashcode, Then they determine the same index position, then judge whether their key is the same, if not the same, then there is a hash conflict.                //hash after the conflict, So HashMap's single bucket store is not a  entry, but a  Entry  chain.                //systems must traverse each  entry sequentially Until you find the  Entry  that you want to search-if the  Entry  that you're searching for is at the very end of the  Entry  chain (which is the first place to put the  bucket ),               // The system must loop to the last to find the element.                if  (e.hash ==  hash &&  ((k = e.key)  == key | |  key.equals (k))  {                    V oldValue = e.value;                    e.value = value;                    return oldvalue ;               }            }           modCount++;            addentry (hash, key, value, i);            return null;        }      

The above program uses an important internal interface: Map.entry, each map.entry is actually a key-value pair. As can be seen from the above program: when the system decides the key-value pair in the storage HashMap, it does not consider the value of Entry, but only calculates and determines the storage location of each Entry according to the key. This also explains the previous conclusion: we can completely take the value of the MAP set as the subordinate of the key, and when the system determines where the key is stored, the value is stored there. HashMap program after my transformation, I deliberately constructed a hash conflict phenomenon, because the initial size of HashMap 16, but I put more than 16 elements inside the HashMap, and I blocked its resize () method. Don't let it go to expansion. The underlying array of HashMap entry[] table structure is as follows:

HashMap inside the bucket appeared in the form of a single linked list, the hash table to solve a problem is the hash value of the conflict, usually two methods: Linked list method and open address method. The list method is to organize the object of the same hash value into a slot with the hash value; The open address method is a detection algorithm that continues to look for the next available slot when a slot is already occupied. Java.util.HashMap using the method of the linked list, the linked list is one-way linked list. The core code to form a single linked list is as follows:

void AddEntry (int hash, K key, V value, int bucketindex) {entry<k,v> e = Table[bucketindex];       Table[bucketindex] = new entry<k,v> (hash, key, value, E);   if (size++ >= threshold) Resize (2 * table.length); Bsp

The code for the above method is simple, but it contains a design: The system always puts the newly added Entry object into the Bucketindex index of the table array-if there is already a Entry object at the Bucketindex index, the newly added Entry object points to the original Entry object (produces a Entry chain), if there is no Entry object at Bucketindex Index, that is, the e variable of the above program code is NULL, that is, the newly placed Entry object points to null, which means that no Entry chain is generated.

HashMap There is no hash conflict, not form a single linked list, HashMap lookup element quickly, get () method can directly navigate to elements, but a single linked list, a single bucket store is not a Entry, but a Entry chain, The system must iterate through each Entry in order until it finds the Entry to search for-if the Entry that is to be searched is at the very end of the Entry chain (the Entry was first placed in the bucket), the system must loop to the last to find the element.

When creating HashMap, there is a default load factor (load factor) with a default value of 0.75, which is a tradeoff between time and space costs: increasing the load factor reduces the memory footprint of the Hash table (that is, the Entry array), but increases the time cost of querying the data , and queries are the most frequent (HashMap get () and put () methods use queries), and reducing load factors increases the performance of data queries, but increases the amount of memory space occupied by the Hash table.

I. Overview of HASHMAP

HashMap the implementation of MAP interface based on hash table. This implementation provides all the optional mapping operations and allows NULL values and NULL keys to be used. (in addition to being unsynchronized and allowing null, the HashMap class is roughly the same as Hashtable.) This class does not guarantee the order of mappings, especially if it does not guarantee that the order is immutable.

It is worth noting that HashMap is not thread-safe, and if you want thread-safe hashmap, you can synchronizedmap get thread-safe HashMap through the static method of the Collections class.

Map map = Collections.synchronizedmap (new HashMap ());

II. data structure of HashMap

The bottom layer of the hashmap is mainly based on arrays and linked lists, and it has a fairly fast query speed because it determines the location of the storage by calculating the hash code. HashMap is mainly through the hashcode of key to calculate the hash value, as long as hashcode the same, calculated hash value is the same. If there are more objects stored, it is possible that the hash values of the different objects are the same, and there is a so-called hash conflict. The students who have studied the data structure know that there are many methods to solve the hash conflict, hashmap the bottom is to solve the hash conflict through the linked list.

In the figure, the purple part represents a hash table, also known as a hash array, each element of the array is a single linked list of the head node, linked list is used to resolve the conflict, if the different key map to the same position in the array, put it into a single list.

Let's look at the code for the entry class in HashMap:

    /** entry is a one-way linked list.    
     * It is the "HashMap chain Storage Method" corresponding to the list. * It implements the Map.entry interface, that is, implementing Getkey (), GetValue (), SetValue (V value), Equals (Object O), hashcode () These functions **/static clas    
        S entry<k,v> implements map.entry<k,v> {final K key;    
        V value;    
        Point to next node entry<k,v> next;    
   
        final int hash;    
        The constructor function. The input parameters include "hash value (h)", "key (k)", "Value (v)", "Next node (n)" Entry (int h, K K, v V, entry<k,v> N) {Valu    
            e = v;    
            Next = n;    
            key = k;    
        hash = h;    
        Public final K Getkey () {return key;    
        Public Final v. GetValue () {return value;    
            Public final V SetValue (v newvalue) {v oldValue = value;    
            value = newvalue;    
        return oldValue; }//Judge two entry isNo equal//returns TRUE if the two entry "key" and "value" are equal. Otherwise, return false to public final Boolean equals (Object o) {if (!) (    
            o instanceof Map.entry) return false;    
            Map.entry e = (map.entry) o;    
            Object K1 = Getkey ();    
            Object K2 = E.getkey (); if (k1 = = K2 | | (K1!= null && k1.equals (K2)))    
                {Object V1 = GetValue ();    
                Object v2 = E.getvalue (); if (v1 = = V2 | |    
                    (v1!= null && v1.equals (v2)))    
            return true;    
        return false; //Implement Hashcode () public final int hashcode () {return (key==null? 0:key).    
        Hashcode ()) ^ (Value==null 0:value.hashcode ());    
        Public final String toString () {return getkey () + "=" + GetValue ();    
}   
        When an element is added to the HashMap, the Recordaccess () is painted. This does not do any processing void recordaccess (hashmap<k,v> m) {}//When the element is deleted from HashMap, the drawing calls the Reco    
        Rdremoval (). No handling void Recordremoval (hashmap<k,v> m) {}}

HashMap is actually a entry array, entry objects contain keys and values, where next is also a entry object, which is used to handle hash conflicts and form a list.

Third, HashMap source analysis

1. Key attributes

Let's take a look at some of the key properties in the HashMap class:

1 Transi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.