Equals summary in Java

Source: Internet
Author: User

Equals summary in Java
I have been using Java for some time. Due to some C ++ skills, I started to write code after a brief look at the Java-related syntax. The result is that a custom class is created, add the custom class to the ArrayList and find whether the ArrayList has this element. I searched the materials online and found that the reason was that the equals () method of the object was not rewritten, and the corresponding object could not be found. After that, the relevant information associated with the contact will be summarized as follows. The form of this summary is to raise a question and then give the answer to the question. This is an attempt to learn knowledge at present, which can make learning more purposeful. Q1. when should I rewrite the equals method of an object? A: When we need to compare values, we need to override the equals method of the object. The exception is clearly described in Article 7th "comply with general conventions when rewriting equals" in objective java. We know that in Java, every Object inherits from the Object. if not overwritten, the default equals code is as follows: public boolean euqals (Object obj) {return this = obj;} can be seen from the above Code, equal uses "=" by default to determine whether two objects are equal. The two objects use "=" to compare the object address. "=" returns true only when the two referenced objects are the same. Therefore, in the example at the beginning, you need to override the equals method so that two objects have equals. Q2. how do I rewrite equals? A: First, when you rewrite the equals method, you must ensure that it meets its general conventions. These conventions are as follows: Self-inverse, for any reference value x, x. equals (x) must be true. Symmetry. For any reference values x and y, if and only if y. equals (x), x. equals (y) must also return true. passed. For any reference values x, y, and z. If x. equals (y) returns true, and y. euqals (z) returns true, x. equals (z) also returns true. Consistency. For any reference values x and y, if the object information used for equals comparison is not modified, the x. equals (y) either returns true consistently or false consistently. non-null. All objects must not be null. In fact, I think a simple method is to refer to the String equals method, which is officially published to meet various requirements. The Code is as follows: public boolean equals (Object anObject) {if (this = anObject) {return true;} if (anObject instanceof String) {String anotherString = (String) anObject; int n = count; if (n = anotherString. count) {char v1 [] = value; char v2 [] = anotherString. value; int I = offset; int j = anotherString. offset; while (n -! = 0) {if (v1 [I ++]! = V2 [j ++]) return false;} return true ;}} return false;} the function is interpreted as follows: use = to check whether the real parameter is a reference to the object ". Use instanceof to check whether the real parameters are of the same type as the object. Different classes are not equal. Convert real parameters to the correct type. Based on the definition of the class, check the conditions that implement equal object values. For more detailed information, see article 7th of objective java, "comply with general conventions when rewriting equals ". Q3. What should I pay attention to when modifying equals? A: pay attention to the following points: If you modify the equals method, modify the hashCode method. First, this is a language convention, one reason for doing this is that when this object acts as an element of the hash container, hashCode is required. The default hashCode of the object is to return a hashCode unique to this object, the hashCode returned values of different objects are different. When processing elements in a hash container, an object is allocated to different buckets Based on the hash value of the object. If we do not override the hashCode of the object, objects with the same value will generate different hash values, so that the corresponding elements cannot be found in the hash container. For more details, see article 8th "hashCode always needs to be rewritten when equals is rewritten" in objective Java ". When rewriting, ensure that the function declaration is correct. Please note that the equals declaration is public boolean equals (Object obj). The parameter type is Object. If the parameter type is Object, it is as follows: class Point {final int x; final int y; public void Point (int x, int y) this. x = x; this. y = y;} public boolean euqals (Point obj) {return (this. x = obj. x & this. y = obj. y) ;}} the following code is executed as expected. Point a (1, 2); Poinr B (1, 2); System. out. println (. equals (B); // outputs true. However, if Class A is put into the container, an import java error occurs. util. hashSet; HashSet <Point> coll = new HashSet <Point> (); coll. add (a); System. out. println (coll. contains (B); // outputs false. This is because the contains method in HashSet calls equals (Object obj), and The equals (Object obj) in Point is still the equals of the Object, as mentioned above, this method compares the object address. Therefore, when contains (B) is called in coll, true is certainly not obtained. When there is an inheritance relationship, pay attention to the correctness of equals. When one class overrides the equals method, another class inherits this class. In this case, the symmetry mentioned above may be violated. The Code is as follows: public class ColoredPoint extends Point {private final Color color; public ColoredPoint (int x, int y, Color color) {super (x, y); this. color = color ;}@ Override public boolean equals (Object other) {boolean result = false; if (other instanceof ColoredPoint) {ColoredPoint that = (ColoredPoint) other; result = (this. color. equals (That. color) & super. equals (that);} return result ;}} when we compare Point p = new Point (1, 2); ColoredPoint cp = new ColoredPoint (1, 2, Color. RED); System. out. println (p. equals (cp); // output tureSystem. out. println (cp. equals (p); // The output is false when the call Point. in equals, only the x and y coordinates of the Point are compared, and ColoredPoint is also of the Point type. Therefore, the code in the third line above is equal. When ColoredPoint is called, the Point is not of the ColoredPoint type, in this case, the fourth line of code outputs false. If we ignore the Color information for comparison, for example, changing the equals method of ColoredPoint to @ overwritepublic boolean equals (Object obj) {if (obj instanceof Point) {return false ;} if (! (Obj instanceof ColoredPoint) {return obj. equals (this);} return super. equals (obj) & (ColoredPoint) obj ). color = color;} in this way, the symmetry is guaranteed, but the transfer is violated, that is, the following situation: ColoredPoint cp1 = new ColoredPoint (1, 2, Color. RED); Point p = new Point (1, 2); ColoredPoint cp2 = new ColoredPoint (1, 2, Color. BLUE); System. out. println (cp1.equals (p); // trueSystem. out. println (p. equals (cp2); // trueSystem. out. println (cp1.equals (cp2 )); // False in the face of this situation, there are roughly two solutions, a cool-shell article-how to avoid the last one of the hidden traps of The equals method in Java, the possibility that the Point and ColoredPoint are equal is cut off. This is a method of processing, and the Point and ColoredPoint are different. Another method is proposed in objective Java, which uses aggregation instead of inheritance to use Point as a member variable of ColoredPoint. At present, I prefer this method because aggregation is more flexible and less coupled than inheritance. The code for this method is as follows: class ColoredPoint {private final Point point; private final Color color; public Point asPoint () {return point;} public boolean equals (Object obj) {boolean ret = false; if (obj instanceof ColoredPoint) {ColoredPoint that = (ColoredPoint) obj; ret = that. point. equals (point) & color. equals (that. color);} return ret;} When ColoredPoint needs to compare coordinates, you can call the asPoint method to convert to coordinates for comparison. In other cases, compare coordinates and colors to solve the above problems about symmetry and transmission. The above is the full text. Due to the limited level, there will inevitably be errors in the article. I hope you can correct them. Thank you.

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.