The Difference and Relation between hashCode and equals

Keywords: Java Attribute

Links to the original text: https://my.oschina.net/u/580135/blog/612339
I. The Role of equals Method

1. By default (without overriding equals method), equals method calls the equals method of Object class, while Object's equals method is mainly used to determine whether the memory address reference of the object is the same address (the same object).

2. If the equals method is covered in the class, then the function of the equals method should be determined according to the specific code. After the coverage, the object is usually judged by whether the content of the object is equal or not.

The equals method code is not overwritten as follows:

  1. //Student class  
  2. public class Student {  
  3.     private int age;  
  4.     private String name;  
  5.       
  6.     public Student() {  
  7.     }  
  8.     public Student(int age, String name) {  
  9.         super();  
  10.         this.age = age;  
  11.         this.name = name;  
  12.     }  
  13.     public int getAge() {  
  14.         return age;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setAge(int age) {  
  20.         this.age = age;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25. }  
The test code is as follows:

  1. import java.util.HashSet;  
  2. import java.util.LinkedList;  
  3. import java.util.Set;  
  4.   
  5.   
  6. public class EqualsTest {  
  7.     public static void main(String[] args) {  
  8.         LinkedList<Student> list = new LinkedList<Student>();  
  9.         Set<Student> set = new HashSet<Student>();  
  10.         Student stu1  = new Student(3,"Zhang San");  
  11.         Student stu2  = new Student(3,"Zhang San");  
  12.         System.out.println("stu1 == stu2 : "+(stu1 == stu2));  
  13.         System.out.println("stu1.equals(stu2) : "+stu1.equals(stu2));  
  14.         list.add(stu1);  
  15.         list.add(stu2);  
  16.         System.out.println("list size:"+ list.size());  
  17.           
  18.         set.add(stu1);  
  19.         set.add(stu2);  
  20.         System.out.println("set size:"+ set.size());  
  21.     }  
  22.   
  23. }  

Operation results:

stu1 == stu2 : false
stu1.equals(stu2) :  false
list size:2
set size:2

Result analysis: Student class does not override equals method, stu1 calls equals method is actually called Object's equals method. So the object memory address is equal to judge whether the object is equal. Because two new objects have different memory addresses, stu1.equals(stu2) is false.

3. Let's override the equals method (age and name attributes) so that the Student class can determine whether the objects are equal by judging whether the contents of the objects are equal.

Overwritten Student class:

  1. //Student class  
  2. public class Student {  
  3.     private int age;  
  4.     private String name;  
  5.       
  6.     public Student() {  
  7.     }  
  8.     public Student(int age, String name) {  
  9.         super();  
  10.         this.age = age;  
  11.         this.name = name;  
  12.     }  
  13.     public int getAge() {  
  14.         return age;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setAge(int age) {  
  20.         this.age = age;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     @Override  
  26.     public boolean equals(Object obj) {  
  27.         if (this == obj)  
  28.             return true;  
  29.         if (obj == null)  
  30.             return false;  
  31.         if (getClass() != obj.getClass())  
  32.             return false;  
  33.         Student other = (Student) obj;  
  34.         if (age != other.age)  
  35.             return false;  
  36.         if (name == null) {  
  37.             if (other.name != null)  
  38.                 return false;  
  39.         } else if (!name.equals(other.name))  
  40.             return false;  
  41.         return true;  
  42.     }  
  43.       
  44. }  

Operation results:

stu1 == stu2 : false
stu1.equals(stu2) : true
list size:2
set size:2

Result analysis: Because the age and name attributes of the two objects of Student are equal, and they are judged by covering equals method. The stu1.equals(stu2) shown is true. Note that the size s of list s and set s tested above are both 2

HashCode

4. By running the above code, we know that the equals method has taken effect. Next, we will cover the hashCode method (which generates hashcode by the age and name attributes) and not the equals method, where the Hash code is generated by age and name.

The Student class after overriding hashcode:

  1. //Student class  
  2. public class Student {  
  3.     private int age;  
  4.     private String name;  
  5.       
  6.     public Student() {  
  7.     }  
  8.     public Student(int age, String name) {  
  9.         super();  
  10.         this.age = age;  
  11.         this.name = name;  
  12.     }  
  13.     public int getAge() {  
  14.         return age;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setAge(int age) {  
  20.         this.age = age;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     @Override  
  26.     public int hashCode() {  
  27.         final int prime = 31;  
  28.         int result = 1;  
  29.         result = prime * result + age;  
  30.         result = prime * result + ((name == null) ? 0 : name.hashCode());  
  31.         return result;  
  32.     }     
  33. }  

Operation results:

stu1 == stu2 : false
stu1.equals(stu2) : false
list size:2
hashCode :775943
hashCode :775943
set size:2

Result analysis: We did not cover equals method but hashCode method. Although the two objects have the same hashCode, when stu1 and stu2 are put into set set set set, because the two objects compared by equals method are false, the hashcode values of the two objects are not compared.

5. Let's cover the equals method and hashCode method.

The Student code is as follows:

  1. //Student class  
  2. public class Student {  
  3.     private int age;  
  4.     private String name;  
  5.     public Student() {  
  6.     }  
  7.     public Student(int age, String name) {  
  8.         super();  
  9.         this.age = age;  
  10.         this.name = name;  
  11.     }  
  12.     public int getAge() {  
  13.         return age;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setAge(int age) {  
  19.         this.age = age;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     @Override  
  25.     public int hashCode() {  
  26.         final int prime = 31;  
  27.         int result = 1;  
  28.         result = prime * result + age;  
  29.         result = prime * result + ((name == null) ? 0 : name.hashCode());  
  30.         System.out.println("hashCode : "+ result);  
  31.         return result;  
  32.     }  
  33.     @Override  
  34.     public boolean equals(Object obj) {  
  35.         if (this == obj)  
  36.             return true;  
  37.         if (obj == null)  
  38.             return false;  
  39.         if (getClass() != obj.getClass())  
  40.             return false;  
  41.         Student other = (Student) obj;  
  42.         if (age != other.age)  
  43.             return false;  
  44.         if (name == null) {  
  45.             if (other.name != null)  
  46.                 return false;  
  47.         } else if (!name.equals(other.name))  
  48.             return false;  
  49.         return true;  
  50.     }  
  51.       
  52. }  


Operation results:

 

stu1 == stu2 : false

stu1.equals(stu2) :true

list size:2

hashCode :775943

hashCode :775943

set size:1

Result analysis: stu1 and stu2 are equal by equals method, and the hashCode value returned is the same, so only one object is put into set set set.

6. Let's make the two object equals methods equal, but hashCode values are not equal.

The code for the Student class is as follows:

  1. //Student class  
  2. public class Student {  
  3.     private int age;  
  4.     private String name;  
  5.     <span style="color:#ff0000;">private static int index=5;</span>  
  6.     public Student() {  
  7.     }  
  8.     public Student(int age, String name) {  
  9.         super();  
  10.         this.age = age;  
  11.         this.name = name;  
  12.     }  
  13.     public int getAge() {  
  14.         return age;  
  15.     }  
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setAge(int age) {  
  20.         this.age = age;  
  21.     }  
  22.     public void setName(String name) {  
  23.         this.name = name;  
  24.     }  
  25.     @Override  
  26.     public int hashCode() {  
  27.         final int prime = 31;  
  28.         int result = 1;  
  29.         result = prime * result + <span style="color:#ff0000;">(age+index++)</span>;  
  30.         result = prime * result + ((name == null) ? 0 : name.hashCode());  
  31.         <span style="color:#ff0000;">System.out.println("result :"+result);</span>  
  32.         return result;  
  33.     }  
  34.     @Override  
  35.     public boolean equals(Object obj) {  
  36.         if (this == obj)  
  37.             return true;  
  38.         if (obj == null)  
  39.             return false;  
  40.         if (getClass() != obj.getClass())  
  41.             return false;  
  42.         Student other = (Student) obj;  
  43.         if (age != other.age)  
  44.             return false;  
  45.         if (name == null) {  
  46.             if (other.name != null)  
  47.                 return false;  
  48.         } else if (!name.equals(other.name))  
  49.             return false;  
  50.         return true;  
  51.     }  
  52.       
  53. }  
Operation results:

stu1 == stu2 : false
stu1.equals(stu2) : true
list size:2
hashCode :776098
hashCode :776129
set size:2

Result analysis: Although stu1 and stu2 are equal by equals method, the hashcode values of the two objects are not equal, so stu1 and stu2 are considered to be two different objects when they are put into set sets.

7. Modify an attribute value of stu1

The Student code is as follows:

  1. //Student class  
  2. public class Student {  
  3.     private int age;  
  4.     private String name;  
  5.     public Student() {  
  6.     }  
  7.     public Student(int age, String name) {  
  8.         super();  
  9.         this.age = age;  
  10.         this.name = name;  
  11.     }  
  12.     public int getAge() {  
  13.         return age;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setAge(int age) {  
  19.         this.age = age;  
  20.     }  
  21.     public void setName(String name) {  
  22.         this.name = name;  
  23.     }  
  24.     @Override  
  25.     public int hashCode() {  
  26.         final int prime = 31;  
  27.         int result = 1;  
  28.         result = prime * result + age;  
  29.         result = prime * result + ((name == null) ? 0 : name.hashCode());  
  30.         System.out.println("hashCode : "+ result);  
  31.         return result;  
  32.     }  
  33.     @Override  
  34.     public boolean equals(Object obj) {  
  35.         if (this == obj)  
  36.             return true;  
  37.         if (obj == null)  
  38.             return false;  
  39.         if (getClass() != obj.getClass())  
  40.             return false;  
  41.         Student other = (Student) obj;  
  42.         if (age != other.age)  
  43.             return false;  
  44.         if (name == null) {  
  45.             if (other.name != null)  
  46.                 return false;  
  47.         } else if (!name.equals(other.name))  
  48.             return false;  
  49.         return true;  
  50.     }  
  51.       
  52. }  

The test code is as follows:

  1. import java.util.HashSet;  
  2. import java.util.LinkedList;  
  3. import java.util.Set;  
  4.   
  5.   
  6. public class EqualsTest {  
  7.     public static void main(String[] args) {  
  8.         LinkedList<Student> list = new LinkedList<Student>();  
  9.         Set<Student> set = new HashSet<Student>();  
  10.         Student stu1  = new Student(3,"Zhang San");  
  11.         Student stu2  = new Student(3,"Zhang San");  
  12.         System.out.println("stu1 == stu2 : "+(stu1 == stu2));  
  13.         System.out.println("stu1.equals(stu2) : "+stu1.equals(stu2));  
  14.         list.add(stu1);  
  15.         list.add(stu2);  
  16.         System.out.println("list size:"+ list.size());  
  17.           
  18.         set.add(stu1);  
  19.         set.add(stu2);  
  20.         System.out.println("set size:"+ set.size());  
  21.         stu1.setAge(34);  
  22.         System.out.println("remove stu1 : "+set.remove(stu1));  
  23.         System.out.println("set size:"+ set.size());  
  24.     }  
  25.   
  26. }  

Operation results:

stu1 == stu2 : false
stu1.equals(stu2) : true
list size:2
hashCode : 775943
hashCode : 775943
set size:1
hashCode : 776904
remove stu1 : false
set size:1

Result analysis:

When we store an object in a set, if the attribute of the object participates in hashcode calculation, then we can not modify those attributes of the object participates in hashcode calculation later, otherwise it will cause unexpected errors. As in testing, stu1 objects cannot be removed.
Conclusion:

1. The equals method is used to compare whether the content of the object is equal (after coverage)

2. hashcode methods are only used in Collections

3. When the equals method is overridden, whether the objects are equal or not will be compared by the equals method after overridden (to judge whether the contents of the objects are equal).

4. When putting an object into a collection, we first determine whether the hashcode value of the object to be put in is equal to that of any element in the collection, and if not, put the object directly into the collection. If the hashcode value is equal, then the equals method is used to determine whether the object to be placed is equal to any object in the set. If the equals judgment is not equal, the element is directly put into the set, otherwise it is not put.

5. A flow chart for putting elements into a collection:




6. The source code of add method in HashSet:

  1. public boolean add(E e) {  
  2.     return map.put(e, PRESENT)==null;  
  3.     }  
map.put source code:
  1. <pre name="code" class="java"public V put(K key, V value) {  
  2.         if (key == null)  
  3.             return putForNullKey(value);  
  4.         int hash = hash(key.hashCode());  
  5.         int i = indexFor(hash, table.length);  
  6.         for (Entry<K,V> e = table[i]; e != null; e = e.next) {  
  7.             Object k;  
  8.             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {  
  9.                 V oldValue = e.value;  
  10.                 e.value = value;  
  11.                 e.recordAccess(this);  
  12.                 return oldValue;  
  13.             }  
  14.         }  
  15.   
  16.         modCount++;  
  17.         addEntry(hash, key, value, i);  
  18.         return null;  
  19.     }</pre>  
  20. <pre></pre>  
  21. <pre></pre>  

Reproduced in: https://my.oschina.net/u/580135/blog/612339

Posted by PBD817 on Thu, 12 Sep 2019 00:08:59 -0700