Java hashmap Analysis II: hash code

Source: Internet
Author: User

Hash calculation is the element in which the computing element should be placed in the array. It is accurate to the linked list to which it is placed. According to Java rules, if you want to put an object into hashmap, your object class must provide the hashcode method and return an integer. For example, the string class has the following method:

 

[Java]View plaincopyprint?

 
  1. Public int hashcode (){
  2. Int H = hash;
  3. Int Len = count;
  4. If (H = 0 & Len> 0 ){
  5. Int off = offset;
  6. Char Val [] = value;
  7. For (INT I = 0; I <Len; I ++ ){
  8. H = 31 * H + val [Off ++];
  9. }
  10. Hash = h;
  11. }
  12. Return h;
  13. }

Pay attention to the for loop above, right? Let me give you an example, so that you can easily understand what it is doing. For example, if there is a string "ABCDE" and the 31-digit calculation method is used to calculate the sum of the string, you will write the following formula:
A * 31 ^ 4 + B * 31 ^ 3 + C * 31 ^ 2 + D * 31 ^ 1 + E * 31 ^ 0. note that here A, B, C, D, or E refer to their ASCII values. An interesting loop can be used to calculate the n-base. This cycle can be extracted separately as a good computing tool:

 

 

[Java]View plaincopyprint?

 
  1. Public static void main (string [] ARGs ){
  2. Int [] A = {1, 0 };
  3. System. Out. println (calculate (2, ));
  4. }
  5. Private Static int calculate (INT Radix, int [] ){
  6. Int sum = 0;
  7. For (INT I = 0; I <A. length; ++ I ){
  8. Sum = sum * Radix + A [I];
  9. }
  10. Return sum;
  11. }


The static caculate method accepts Radix as the base number of the base number. array a simulates the number of the base number to be calculated, but the surface sequence must be consistent. For example, the 01 binary string should be arranged in the array by {0, 1. The output result above is 1, which matches the actual value of 01.
So why choose 31 as the base? First, you need to understand why hashcode is needed. each object calculates the hashcode based on the value. Although the code size is not expected to be unique (because the calculation is usually very slow), do not repeat it as much as possible, so the Base should be as large as possible. In addition, 31 * N can be optimized
Move 5 digits left and then subtract 1, which has high performance. In fact, the choice of 31 or controversial opponents (refer to the http://stackoverflow.com/questions/299304/why-does-javas-hashcode-in-string-use-31-as-a-multiplier)
I think this will still lead to a large number of duplicates and should use a larger number. Therefore, Java implementations may change in the future. The following article introduces two conclusions:
1. The base must be a prime number.
The property of prime numbers (only 1 is a factor with itself) makes it easier to produce uniqueness after multiplying it with other numbers, that is, the minimum probability of conflict between hash code values.
2. Selecting 31 is an option after the observed distribution result. The reason is unclear, but it is indeed advantageous.

Http://computinglife.wordpress.com/2008/11/20/why-do-hash-functions-use-prime-numbers/

In addition, String. hashcode caches the value calculated for the first time, because this is a final (immutable) class, that is, the content of the string object will not change. This can improve performance when put to hashmap for multiple times, but it does not seem to be of much use.

 

Now, we have finally finished the topic of string. hashcode. Now, return to the hashmap array element position calculation.

Related Article

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.