"Java programming" correctly rewrites Hashcode and EQUESL scenarios

Source: Internet
Author: User

First, the correct way to write Hashcode:

The "principle" overrides the Hashcode () by using the attribute in Equals () to compare whether two objects are in a consistent condition.

{1}. A common approach is to use a linear combination of the attributes involved.

{2}. The combination coefficients involved in the linear combination process can be customized.

Note that the value after stitching cannot exceed the range of the shaping.

{3}. formula: int form of attribute 1 + c1* attribute 2 int form + c2* Property 3 int form + ...

"Tip" When a property is a reference type, if Hashcode () has been overridden, then the int form of the reference property is the direct invocation of the property's existing hashcode value.

The most typical is that this property is a string type, and the string type has rewritten the hashcode () method, so it can be used directly.

"Java programming" correctly rewrites Hashcode and EQUESL scenarios

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.