Chapter 3: common methods for all objects. Item8: Use the generic conventions to override equals.

Source: Internet
Author: User

1. When do I need to overwrite equals? If a class has its own unique "logical equality" concept, and the superclass does not overwrite equals.

2. Conventions to be observed when covering equals:

  • Self-defense. For any non-null reference value X, X. Equals (x) must return true.
  • Symmetry. For any non-null reference values X and Y, if and only if y. Equals (x) returns true, X. Equals (y) must return true.
1 package COM. twoslow. cha3; 2 3/** 4 * symmetry 5 */6 public class caseinsensitivestring {7 8 private final string s; 9 10 public caseinsensitivestring (string S) {11 if (S = NULL) 12 throw new nullpointerexception (); 13 this. S = s; 14} 15 16 @ override17 public Boolean equals (Object O) {18/* 19 * methods in the string class do not know strings of the caseinsensitivestring type, always return false, 20 * If (O instanceof caseinsensitivestring) 21 return S. equalsignorecase (caseinsensitivestring) O ). s); 22 if (O instanceof string) 23 return S. equalsignorecase (string) O); 24 */25/** 26 * remove the code for interoperation with string 27 */28 return (O instanceof caseinsensitivestring) & (caseinsensitivestring) O ). s. equalsignorecase (s); 29} 30 31}

 

  • Transmission. For any non-null reference values X, Y, and Z, if X. equals (y) returns true, and Y. if equals (z) returns true, X. equals (z) must return true.
1 package COM. twoslow. cha3; 2 3 Import Java. AWT. color; 4 5 public class colorpoint extends point {6 7 private final color; 8 9 Public colorpoint (int x, int y, color) {10 super (x, y ); 11 This. color = color; 12} 13 14/** 15 * when comparing common and colored points, or the opposite, the former ignores color 16 *, and the latter always returns false17 */18 @ override19 public Boolean equals (Object O) {20 if (! (O instanceof colorpoint) 21 return false; 22 return Super. equals (o) & (colorpoint) O ). color = color; 23} 24 25} 26 27 28 package COM. twoslow. cha3; 29 30 Import Java. AWT. color; 31 32/** 33 * combination takes precedence over 34 */35 public class colorpoint02 {36 37 private final point; 38 private final color; 39 40 public colorpoint02 (int x, int y, color) {41 if (color = NULL) 42 throw new nullpointer1_tio N (); 43 point = new point (x, y); 44 This. Color = color; 45} 46 // provides a view method to return an internal point object instance. Here, the point instance is a final object, which is very important. 47 // you can avoid incorrect changes. View methods are widely used in Java Collection frameworks. 48 Public point aspoint () {49 return point; 50} 51 52 @ override53 public Boolean equals (Object O) {54 if (! (O instanceof colorpoint02) 55 return false; 56 colorpoint02 Other = (colorpoint02) O; 57 Return Other. point. equals (point) & Other. color. equals (color); 58} 59}

 

  • Consistency. For any non-null reference values X and Y, as long as the information used in the comparison operation of equals in the object is not modified, multiple calls of X. equals (y) returns true or false consistently.
  • For any non-null reference value X, X. Equals (null) must return false.
1 @ override public Boolean equals (Object O) {2 if (O = NULL) 3 return false; 4 If (! (O instanceof mytype) 5 return false; 6} 7 8 // pay attention to the instanceof judgment in the above Code, because the parameter o needs to be strongly converted in the subsequent implementation, if the type is not 9 // match, classcastexception is thrown, causing the equals method to exit early. It should be noted that the instanceof has another 10 // potential rule. If its left value is null, The instanceof operator will always return false, so the above Code can be optimized: 11 @ override public Boolean equals (Object O) {12 if (! (O instanceof mytype) 13 return false; 14}

 

Given the above descriptions, this entry provides the best logic for reloading the equals method:
1. Use the = Operator to check whether the parameter is a reference to this object. If yes, true is returned. Since the = Operator is based on the object address comparison, this is a performance optimization method especially for objects with complex and Comparative logic.
2. Use the instanceof operator to check whether the parameter is of the correct type. If not, false is returned.
3. Convert the parameter to the correct type. Because you have passed the instanceof test, classcastexception is not thrown.
4. For each "key" field in the class, check whether the field in the parameter matches the corresponding field in the object.

5. if the field of the domain is an object, use the equals method to compare the equality between the two. If the field is of the basic integer type such as int, you can directly compare it. If the field is of the floating point type, considering the precision and double. nan
For issues such as float. Nan, we recommend that you use the compare method of its corresponding packaging class. If it is an array, you can use the arrays. Equals method added in JDK 1.5. As we all know, the & operator has a short circuit principle, so we should most likely compare the Similarities and Differences
Lower-Sales domains are placed at the forefront.

Chapter 3: common methods for all objects. Item8: Use the generic conventions to override equals.

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.