“This is the 10th day of my participation in the August Gwen Challenge.


Object of the equals ()

The equals method of object is used to compare whether two objects are equal.

public boolean equals(Object obj) {
        return (this == obj);
    }
Copy the code

Objects have a memory address and its specific content, and the equals method in the object is to compare the two object memory address is the same, namely obj1. Equals (obj2) is true, it is said both refer to the same object, but in actual development, most is the comparison between two objects, So we need to override equals (String, Math, etc.) as we see fit

The equals method overrides follow the rule

  1. Reflexivity: x.equals(x) should return true for any non-null reference value x.
  2. Symmetry: for any non-null reference values x and y, x.cube (y) should return true if and only if y.cube (x) returns true.
  3. Transitivity: For any non-null reference values x, y, and z, x. quals(z) should return true if x. quals(y) returns true and y.quals (z) returns true.
  4. Consistency: Multiple calls to x.equals(y) always return true or always return false for any non-null reference values x and y, provided that the information used in the equals comparison on the object has not been modified.
  5. For any non-null reference value x, x.equals(null) should return false.

Override the equals method implementation

  1. Create an ObjectTest1 class
/** * Created by lirui on 2018/12/12. */ public class ObjectTest1 { private Integer size; private String name; private String des; public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDes() { return des; } public void setDes(String des) { this.des = des; }}Copy the code
  1. Creating a test class
public class EqualsDemo { public static void main(String[] args) { ObjectTest1 objectTest1=new ObjectTest1(); objectTest1.setName("wahh"); ObjectTest1 objectTest12=new ObjectTest1(); objectTest12.setName("wahh"); System. The out. Println (" = - = - = - = - = - = - = - = run results - = - = - = - = - = - = - = - = "); System.out.println(objectTest1.equals(objectTest12)); }} - = - = - = - = - = - = - = - = run results - = - = - = - = - = - = - = - = false Process finished with exit code 0Copy the code

Equals () = false (Object) = false (Object) = false (Object

  1. Override equals
/** * Created by lirui on 2018/12/12. */ public class ObjectTest1 { private Integer size; private String name; private String des; public Integer getSize() { return size; } public void setSize(Integer size) { this.size = size; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } @Override public boolean equals(Object obj) { if (this==obj){ return true; } if (obj instanceof ObjectTest1){ ObjectTest1 objectTest1=(ObjectTest1) obj; return name.equals(objectTest1.name); }else{ return false; }}}Copy the code
  1. Test class results
/** * Created by lirui on 2018/12/12. */ public class EqualsDemo { public static void main(String[] args) { ObjectTest1 objectTest1=new ObjectTest1(); objectTest1.setName("wahh"); ObjectTest1 objectTest12=new ObjectTest1(); objectTest12.setName("wahh"); System. The out. Println (" = - = - = - = - = - = - = - = run results - = - = - = - = - = - = - = - = "); System.out.println(objectTest1.equals(objectTest12)); }} -=-=-=--=-=-=-=-= -= run results -=-=-=-=-=-=-=-= -=-=-= trueCopy the code

If true is returned, override equals to determine whether the two objects have the same memory address, then determine if the left object is an instanceof the right object based on the instanceof keyword, and if so, whether the two objects have the same name

  1. Create ObjectTestChild class inheriting ObjectTest1 using getClass in equals()
/** * Created by lirui on 2018/12/13. */ public class ObjectTestChild extends ObjectTest1{ String test; public ObjectTestChild(String name,String test) { super(name); this.test=test; } public String getTest() { return test; } public void setTest(String test) { this.test = test; } @Override public boolean equals(Object obj) { if (obj==this) return true; if (obj instanceof ObjectTestChild){ ObjectTestChild objectTestChild=(ObjectTestChild) obj; return super.equals(objectTestChild)&&objectTestChild.test==test; } return false; }}Copy the code

The subclass overrides equals and its parent. The subclass has only one more test attribute than its parent

public class EqualsDemo { public static void main(String[] args) { ObjectTest1 objectTest1 = new ObjectTest1(); objectTest1.setName("wahaha"); ObjectTestChild child1 = new ObjectTestChild("wahaha", "123"); ObjectTestChild child2 = new ObjectTestChild("wahaha", "456"); System. The out. Println (" = - = - = - = - = - = - = - = run results - = - = - = - = - = - = - = - = "); System.out.println(objectTest1.equals(child1)); System.out.println(objectTest1.equals(child2)); System.out.println(child1.equals(child2)); }} - = - = - = - = - = - = - = - = run results - = - = - = - = - = - = - = - = true true or falseCopy the code

Equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals The reason for this is that we use the instanceof keyword for comparison. Since the subclass inherits from the parent class, the left side must be equal to the right side of the instance, and both names are equal. Therefore, it is recommended to use getClass for type determination when overwriting equals. Instead of using Instanceof, the getClass method returns the equals override method of the runtime class modification parent and subclass

@override public Boolean equals(Object obj){if (this==obj){return true; } if (obj.getClass()==this.getClass()){ ObjectTest1 objectTest1=(ObjectTest1) obj; return name.equals(objectTest1.name); }else{ return false; }} Subclass public Boolean equals(Object obj) {if (obj==this) return true; if (obj.getClass()==this.getClass()){ ObjectTestChild objectTestChild=(ObjectTestChild) obj; return super.equals(objectTestChild)&&objectTestChild.test==test; } return false; }Copy the code

The test program

public class EqualsDemo { public static void main(String[] args) { ObjectTest1 objectTest1 = new ObjectTest1(); objectTest1.setName("wahaha"); ObjectTestChild child1 = new ObjectTestChild("wahaha", "123"); ObjectTestChild child2 = new ObjectTestChild("wahaha", "456"); System. The out. Println (" = - = - = - = - = - = - = - = run results - = - = - = - = - = - = - = - = "); System.out.println(objectTest1.equals(child1)); System.out.println(objectTest1.equals(child2)); System.out.println(child1.equals(child2)); }} - = - = - = - = - = - = - = - = run results - = - = - = - = - = - = - = - = false false falseCopy the code

Why override hashCode when overriding equals

First, the equals method has the following relationship to hashCode

  1. If two objects are the same (that is, the equals comparison returns true), they have the same hashCode
  2. If two objects compare the same hashCode, they are not necessarily the same (i.e., equals comparison returns false)

Why override the hashCode method

Since hashCode is implemented to improve the efficiency of the program, compare hashCode first. If it is different, then there is no need to compare equals. This greatly reduces the number of equals comparisons. A good example is the use of collections;

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Java uses hash tables. Using a hash algorithm (also known as a hash algorithm), an object data is defined to an address using a specific algorithm based on the characteristics of the object. = = = = = = = = = = = = = = = = = = =

The hashcode method must be overridden to ensure that the same object has the same hashcode value as equals. If you overwrite equals without overwriting hashcode, It is possible that two unrelated objects are equal (because equal is overridden based on the characteristics of the object), but HashCode is different.