The hashcode () function of the Java string class

Source: Internet
Author: User

1 = = and Equals ()

= = Compares whether two objects point to the same memory address.

The Equals () method determines whether the values of two objects are equal.

2 String.Equals ()

The string class overrides the Equals () method as follows:

1      Public Booleanequals (Object anobject) {2         if( This==anobject) {3             return true;4         }5         if(AnObjectinstanceofString) {6String anotherstring =(String) anobject;7             intn =value.length;8             if(n = =anotherString.value.length) {9                 CharV1[] =value;Ten                 CharV2[] =Anotherstring.value; One                 inti = 0; A                  while(n--! = 0) { -                     if(V1[i]! =V2[i]) -                             return false; thei++; -                 } -                 return true; -             } +         } -         return false; +}

String, StringBuffer, and StringBuilder are 22 not equals ().

3 String.hashcode ()

When you determine whether an object's values are equal, you first determine whether their hashcode are equal. If the hashcode is not equal, the Equals () method returns false, otherwise it is compared.

The JVM default hashcode are not equal, and the object values are not equal. Its inverse proposition is: if the object value is equal, then its hashcode is equal.

When overriding Equals (), you must also override Hashcode (). And you need to ensure that objects of equal value have the same hashcode.

1      Public inthashcode () {2         inth =Hash;3         if(h = = 0 && value.length > 0) {4             CharVal[] =value;5 6              for(inti = 0; i < value.length; i++) {7H = * H +Val[i];8             }9hash =h;Ten         } One         returnh; A}

The reason for the magic number 31 here is

* Because multiplying by 2 is a shift calculation, the information is lost when the multiplication overflows, so you cannot choose multiples of 2 and 2 (the same effect as 2k and K, but 2k is more computationally).

* Primes can make the distribution of the hash more uniform.

* 31 is the number of primes greater than 26, the easiest to calculate (shift subtraction).

The meaning of the first and third articles here is self-existing, and the second, there is no clear proof.

At least under some hashcode calculation methods, composite is not more prone to conflict than prime numbers.

For example, Hashcode = value% size

The hashcode () function of the Java string class

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.