The relationship between equals and hashcode in Java learning

Source: Internet
Author: User

Two object values are the same (x.equals (y) = = true), but can have different hash code, this sentence right?
A: No, if two objects x and y satisfy x.equals (y) = = True, their hash code (hash code) should be the same. Java for the Eqauls method and Hashcode method is this: (1) If two objects are the same (the Equals method returns True), then their hashcode values must be the same, (2) If two objects have the same hashcode, they are not necessarily the same. Of course, you may not have to do as required, but if you violate the above principles you will find that when using containers, the same objects can appear in the set set, while the efficiency of adding new elements is greatly reduced (for systems using hash storage, if the hash code conflicts frequently, the access performance will be drastically reduced).

add:The Equals method: First the Equals method must satisfy the reflexivity (x.equals (x) must return True), the symmetry (x.equals (Y) returns True, Y.equals (x) must also return true), When transitivity (X.equals (y) and Y.equals (z) both return true, X.equals (z) must also return true) and consistency (when the object information referenced by x and Y is not modified, multiple calls to X.equals (y) should get the same return value), and the reference x,x.equals (NULL) for any non-null value must return FALSE. The tips for achieving a high-quality Equals method include: 1. Use the = = operator to check if the parameter is a reference to this object; 2. Use the instanceof operator to check if the parameter is the correct type; 3. For the key properties in the class, check that the properties of the parameter's incoming object match; 4. After writing the Equals method, ask yourself whether it satisfies symmetry, transitivity, consistency; 5. Overriding equals always overrides Hashcode;6. Do not replace object objects in the Equals method argument with other types, and do not forget the @override annotations when overriding.

The relationship between equals and hashcode in Java learning

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.