Determining whether two objects are equivalent is a common requirement in OOP programming (described below in Java).
Consider a number of situations in which two objects are "equivalent" by a certain eigenvalue, and when the two objects are equivalent, the result is true, otherwise the result is false.
Of course, the "eigenvalues" here are not just simple "object references", in fact, the Equals method implemented in the object class (the root of the "object World" of Java) is to set the "eigenvalue" to "object reference" for the equivalence of the judgment, so it can be learned that The Equals method in the object class simply returns the value of the "= = operation" of this reference and the reference of the judged obj.
However, in many cases, it is not required that two objects only refer to the same time (at which point they are an object) to be "equal", which requires the ADT Designer to define the equivalent of two instance objects, that is, to set the eigenvalues to be compared.
For example, in a social software requires that each user's username (name) must be unique, then each time a new user is added to the user's registration name to judge, if the current user name is already occupied, you can not create an account for the user, only require the new user to re-select the user name.
At this point, you can determine whether the eigenvalues of two objects are named name (you might set it to a string-type private property), and this implementation is simulated.
Here is a description of the code in equals:
The first step is to determine if the reference values are equal, and at this point Person1.equals (Person1), you can quickly return the result true.
The second step is to determine if the type matches, if two objects are equivalent, if they must be of the same type, at which point person1.equals (null) can be judged and return the result false.
The third step is to determine the equivalence of objects according to the predetermined eigenvalues.
Operation Result:
Here are some points to note:
1. The Equals method in the class must be overridden/overwritten (override), because it is required by the design to determine the equivalence according to the eigenvalue.
The eigenvalues here are the string type Name property, which represents the name of each person object. Since only the characteristic value that needs to be compared is set in the Equals method, so long as the name of the two Person class object is the same, their judgment is the same.
2. The Hashcode method in the class needs to be overridden/overwritten
In fact, when 1 is achieved, it is guaranteed to determine whether the equivalence of two objects has been established (at this point the Person1.equals (Person2) value in the program is guaranteed to be true. However, the Equals method obtained is very restrictive. For example, to add Person1 to a hashset, at this time to determine whether HashSet contains Person2, because at design time, the eigenvalues are only name, then the expected Hashset.contains (Person2) value should also be true, However, if you do not implement the Hashcode method, the return value can only be false.
For this reason, the stored procedure of each instance object in Java can be thought of as "throwing the data of this object into a bucket", and in order to compare it faster, it divides the space of the whole program into quite a lot of "buckets", and numbers each bucket, the data that is loaded in the bucket, There is a requirement that each instance object be numbered, and only two objects of the same number can be assigned to a bucket. In this way, to determine whether the two objects are equivalent (that is, if the Equals method returns True), you only need to access the bucket, because the two objects must appear in the same bucket. Step 1 has implemented "after finding two objects, judging by a certain eigenvalue", but did not implement "let two objects in a bucket". This is the key to the problem. So in order to ensure that two objects are assigned to the same "bucket", it is necessary to rewrite their hashcode method, which is implemented by default in Java for each type of hashcode method. The following implementation of the Hashcode code, because the eigenvalues are name, in order to ensure that the two person class objects are equivalent, then their name must be the same, considering that the name (sting type) has been implemented Hashcode, Simply return the hashcode value of their name. This guarantees that if the name of the two person object is the same, then their hashcode must be the same, and it will be easier to judge next.
Note: The emphasis is on understanding this "bucket" concept, through this abstraction process, it is also possible to understand that "two equivalent objects in Java must have the same hashcode value, but two have the same hashcode value of the object is not necessarily equivalent" this sentence. The point of this sentence is to consider the details of how the bucket is loaded, and what type of object it "loads".
This gives the person class that does not implement Hashcode and shows its test code:
Test results:
Visible, when Hashcode is not implemented, Set.contains (Person2) is false, that is, when the HashSet type retrieves person2, it finds that it is not in the "bucket" where its load object (PEROSN1) is located, and returns false directly.
At this point, re-implement the code:
Test the above test code again at this time, test results:
As you can see, although the test set does not load Person2, the Person2 is also judged to be equivalent in accordance with the overridden equals rule, so after the hashcode is implemented, it is also possible for the holding object to determine its equivalence according to the rules set.
Of course, the above implementation code and testing are based on the eigenvalue for the implementation of the name, in real life, such as "identity card" to determine whether two objects are "equivalent" (that is, whether it is the same person), the eigenvalues will naturally include name (name), sex (gender), age (ages) and other attributes , taking into account the frequent use of identity cards and a wide range of applications, each inhabitant rightfully has an additional "attribute": a Social Security number. This unique value makes it possible to distinguish between each object, and to easily sort (and retrieve operations).
This shows that the real life everywhere embodies "ADT Designer's Wisdom" ...
Java provides program developers with a flexible way to set "eigenvalues", so when designing a required data type, it is possible to think carefully about the basis (eigenvalue) in which two objects are judged to be equivalent, so that the Equals method, which is implemented, often brings great convenience to the use of ADT.
From Steven Shen
Edited on 2018.6.19
The implementation of the Equals method in Java and its details