The Java Foundation equals () and Hashcode ()

Source: Internet
Author: User

Equals () is one of the methods in object:

1  Public Boolean equals (Object obj) {2         return ( this = = obj); 3     }

In object, the Equals () method returns results that are identical to the result of the = = return. However, it is overridden in subclasses, such as String

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; +}

At this point, the meaning of the representation is that as long as two objects have the same value (field), the result of the Equals () of the two objects is the same, and this is a common use of equals ().

The Hashcode () method in Objecct,

The Hashcode () method acts as a hash of the object, and in the Hashmap,hashset,hashset collection, the effect of the random access element can be achieved. The use of hash values (Hashcode ()) reduces the number of comparisons between elements as much as possible.

1  Public native int hashcode ();

About the Hashcode () methods are:

1. Overriding the Equals () method of a class, you must override the Hashcode () method of the class;

The 2.equals () method returns True to be a sufficient condition for hashcode () to return the same value. (This is also the relationship between the Equals () method and the Hashcode () method)

The following is the implementation of the Hashcode () method of the string:

In this implementation, the value of Hashcode () is a value mapping in the string object, combined with the implementation of the Equals () method of string, the implementation of the two methods relies on the field value of string, so the results are consistent.

1  Public inthashcode () {2         inth =Hash;//hash Default value is 03         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 Java Foundation equals () and Hashcode ()

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.