Two object values are the same (x.equals (y) = = true), but can have different hash code, this sentence right?

Source: Internet
Author: User

For:

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: About Equals and Hashcode methods, many Java programs are known, but many people just know, in Joshua Bloch's masterpiece "Effective Java" (Many software companies, "effective Java", " Java Programming Ideas and refactoring: Improving existing code quality is the Java programmer must read books, if you have not seen, then quickly go to the Amazon to buy a copy of it is described in this way: first the Equals method must satisfy the reflexivity (x.equals (x) Must return True), symmetry (x.equals (Y) returns True when Y.equals (x) must also return true), transitivity (X.equals (y) and y.equals (z) all return True when 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 a reference to any non-null value x,x.equals (NULL) 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.

Two object values are the same (x.equals (y) = = true), but can have different hash code, is that right?

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.