Implementation principle of HashMap in hash algorithm &&java __ Code

Source: Internet
Author: User



The HashMap is implemented through a entry array. The entry structure has three properties, Key,value,next. If in C, we encounter next think of the inevitable pointer, actually in Java this is a pointer. Hashes store data each time through the value of the hashcode. and Hashcode () The simplest method of breaking the law is:

hash algorithm in string:

public int hashcode () {  
       int h = hash;  
       if (h = = 0 && value.length > 0) {  
           char val[] = value;  
  
           for (int i = 0; i < value.length i++) {  
               h = * H + val[i];  
           }  
           hash = h;  
       }  
       return h;  
   }  


Why 31 as a multiplier, I have been not very clear ... I'd appreciate it if you knew someone to ask for advice.


For example, a string "hash", the ASCII of each character of the word, H 104,a 97,s 115:

h = 0;

H1 = h*31+104 = 0*31+104 = 104

h2=h1*31+97=104*31+97=3321

h3=h2*31+115=3321*31+115=103066

h4=h3*31+104=103066*31+104=3195150




A hash value is obtained by the above algorithm, and then a hash bucket is obtained, which is used to hold the value pointing to the hash bucket, so it can also be called a hashing field.

int index = (hash & 0x7fffffff)% length;
</pre><pre code_snippet_id= "1635422" snippet_file_name= "blog_20160405_2_9723445" name= "code" class= "Java" >
</pre><pre code_snippet_id= "1635422" snippet_file_name= "blog_20160405_2_9723445" name= "code" class= "Java" ><pre name= "code" class= "Java" >//storage:
int hash = Key.hashcode ();//This hashcode method is not detailed here, Just understand that the hash of each key is a fixed int value
int index = hash% Entry[].length;
Entry[index] = value;

Value:
int hash = Key.hashcode ();
int index = hash% Entry[].length;
return Entry[index];


This hash field is actually a pointer list. As mentioned above, HashMap is actually an array of entry<e >, the subscript pointer of this array is actually a row, and the field of a hash bucket. The next pointer in the entry is actually the content of the line and the container of the bucket, which is a linked list, so the capacity can vary flexibly.

Therefore, HashMap is actually the image of the classification of an array, and each subscript of the array represents a bucket, each bucket is a field, each time looking for a value, as long as the first count of the content of the hashcode, you can know in which domain, which will save a lot of iterative process.

In our usual process, the Equals () and the hashcode () need to be made simultaneously. As we all know, the class that inherits the object actually needs to copy equals (), otherwise the method itself is actually the role of ' = = '. However, in the replication of the Equals method, it should be noted that the uniqueness of the hashcode must be ensured. It is also important to ensure the uniqueness of a hashcode content. So many times, when you copy equals, you also have to make a hashcode method.

This is the Equals method in string:

public boolean equals (Object anobject) {  
     if (this = = AnObject) {return  
         true;  
     }  
     if (anobject instanceof string) {  
         string anotherstring = (string) anobject;  
         int n = value.length;  
         if (n = = anotherString.value.length) {  
             char v1[] = value;  
             Char v2[] = Anotherstring.value;  
             int i = 0;  
             while (n--! = 0) {  
                 if (V1[i]!= v2[i]) return  
                         false;  
                 i++;  
             }  
             return true;  
         }  
     return false;  
 }  



In HashMap, the process of determining whether two values are equal is:

* * To determine whether this two object reference is the same, if the same, the description is an object, return true;

* * Calculate whether the hashcode of this two string is the same (in the initial replication of equals, you should combine Hashcode (), which is mutually verified), if not the same, return false;

* * To compare the two string contents, bitwise by-bit, knowing that the last content is exactly the same, returns True, otherwise, false


*****************************************************************************************************

This is the end of the analysis.

Part of the above reference from: http://blog.csdn.net/vking_wang/article/details/14166593



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.