Comparison of hashCode() and equals() methods

    Java specifies the equals method and hashCode method as follows:

       1. If two objects are the same, their hashCode values ​​must be the same;

       2. If the hashCode of two objects is the same, they are not necessarily the same (the same objects mentioned above refer to the comparison using the equals method.)

       equals is a method of the Object class, which is used to compare whether two objects are equal. The default equals method of the Object class is to compare the addresses of the two objects;

       hashCode is also a method of the Object class. Returns a discrete int integer. Used in collection class operations to improve query speed.

       In the Java language, the use of equals() and hashCode() is closely coordinated. If you design one of them yourself, you must design the other. In most cases, these two functions are not considered, and their default design can be used directly. However, in some cases, these two functions are best designed by themselves to ensure the normal operation of the entire program. The most common is when an object is added to the collection object, these two functions must be designed by themselves. A more refined definition is: if you want to put an object A into another collection object B, or use this object A as the key to find the location of a meta object in collection object B, and support whether to accommodate, delete the collection object For operations such as the meta object in B, the equals() and hashCode() functions must be defined by the developer. In other cases, these two functions do not need to be defined.

       equals():

       It is used to compare two objects, the content of the object, and of course, the comparison of the reference value of the object. What is object reference value comparison? It is the worth comparison of two reference variables. The value of the reference variable is actually a number. This number can be regarded as a code to identify different objects. The comparison of the reference value of two objects is the comparison of two numbers and the comparison of two codes. This comparison is the default object comparison method. In the Object object, this method has been designed. So you don't have to rewrite it yourself, wasting unnecessary time. The comparison of object content is the real purpose of designing equals(). The requirements of the Java language for equals() are as follows, and these requirements must be followed:

      • Symmetry: If x.equals(y) returns "true", then y.equals(x) should also return "true".

      • Reflective: x.equals(x) must return "true".

      • Analogy: If x.equals(y) returns "true", and y.equals(z) returns "true", then z.equals(x) should also return "true".

      • There is also consistency: if x.equals(y) returns "true", as long as the contents of x and y remain unchanged, no matter how many times you repeat x.equals(y), the return is "true".

      • In any case , x.equals(null) will always return "false"; x.equals (objects of different types from x) will always return "false".

     hashCode():
       This function returns an integer code used for the Hirsch operation. The value returned by hashCode() is used to classify the position of the object in some specific collection object. These objects are HashMap, Hashtable, HashSet, etc. This function and the above equals() function must be designed by yourself to assist HashMap, Hashtable, HashSet, etc. to search and locate a large number of objects collected by themselves. How these collection objects work, imagine that each meta-object hashCode is the code of a box. According to the code, each meta-object is classified into the corresponding box according to the code provided by hashCode(). All the boxes add up to a HashSet, HashMap, or Hashtable object. When we need to find a meta object, we first look at its code, which is the integer value returned by hashCode(), so that we find the box it is in, and then in the box , each meta-object is taken out one by one and compared with the object we are looking for, if the contents of the two objects are equal, our search is over. This operation requires two important pieces of information, one is the hashCode() of the object, and the other is the result of the object content comparison.

     The relationship between the return value of hashCode() and equals() is as follows:

       If the result obtained by calling the equals method is true, the hashcode values ​​of the two objects must be equal; 
       if the result obtained by the equals method is false, the hashcode values ​​of the two objects are not necessarily different; 
       if the hashcode values ​​of the two objects are not equal, The result obtained by the equals method must be false; 
       if the hashcode values ​​of the two objects are equal, the result obtained by the equals method is unknown

The reason why these two rules are like this is actually very simple. Take HashSet as an example. HashSet can have one or more boxes, and there can be one or more unique meta objects in the same box (the HashSet contains must be a unique meta object). This example shows that a meta-object can have the same hashCode as a different meta-object. But a meta-object can only be equal to a meta-object with the same content. So these two rules must hold.

     Summarize

       In Java's collections, the rules for judging whether two objects are equal are: 

           1. Determine whether the hashCode of two objects are equal:

                ① If they are not equal, consider that the two objects are also not equal, and complete 

                ②If they are equal, turn into 2 ( this is only required to improve storage efficiency. In fact, it is not necessary in theory, but if not, the efficiency will be greatly reduced in actual use, so we will make it necessary here. Focus on this issue.) 

           2. Judging whether two objects are equal using the equals operation: 
                ① If they are not equal, consider the two objects are also not equal. 
                ② If they are equal, consider the two objects equal (equals() is the key to judging whether two objects are equal) 

            Reference: http://www.jb51.net/article/42278.htm

                        https://blog.csdn.net/u012151556/article/details/53912794

                        https://blog.csdn.net/qq598535550/article/details/40407855

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325303132&siteId=291194637