High-quality Equals method and Hashcode

Source: Internet
Author: User

Equals method

    1. Use the = = operator to check whether the parameter is a reference to the current object, and if so, to return true directly;
    2. Use the instanceof operator to check if the parameter is the correct type. If not, return false directly. (depending on the logic, you may need to use = = To determine the Class);
    3. Converts the parameter to the correct type;
    4. For each critical field in the class, check to see if the field in the parameter matches the fish in the object. For base type fields that are neither float nor double, use = = Compare, use the Equals method for object reference fields, use Float.compare for float fields, and for double fields, use Double.compare
Note the point:
    • When overriding equals, always overwrite hashcode;
    • Do not replace the object parameter type in equals (Object obj) with another type.
hash function of the Hashcode method:
      1. Save a nonzero constant value, such as 17, in a variable of type int named result;
      2. Complete the following steps for each critical field F in the object, which refers to each domain that is involved in the Equals method:
        1. Computes the hash code C for the int type for the field:
          1. If the field is a Boolean type, the calculation (f?1:0);
          2. Evaluates (int) F If the field is a byte, short, char, or int type;
          3. If the field is a long type, the calculation (int) (f^ (f>>>32));
          4. If the field is of type float, the float.floattointbits (f) is computed;
          5. If the field is a double type, calculate Double.doubletolongbits (f), and then follow step 2.1.3 to calculate the hash value for the resulting long type;
          6. If the field is an object reference, and the Equals method of the class compares the domain by a recursive call to equals, then universal calls Hashcode for the domain recursively.
          7. If the field is an array, each element should be treated as a separate domain.
        2. Follow the formula below to merge the hash code C calculated in step 2.1 into result:
          result = 31*result + C;
      3. return result

High-quality Equals method and Hashcode

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.