Why override equals must override the hashCode method?

As we all know, it equals and hashcode are two important methods java.lang.Object class in practical applications often need to override both methods, but as to why these two methods to rewrite a lot of people do not understand.

Below we look to achieve the Object class default equals and hashCode methods:

1     public boolean equals(Object obj) {
2         return (this == obj);
3     }
4 
5     public native int hashCode();

 

  

These are the Object class source on these two methods, Object class equals the default rule is to compare the memory address comparing two objects. The hashcode is a local method, but in fact, according to the memory address via a hashcode hash algorithm object comes from.

 1 import lombok.AllArgsConstructor;
 2 import lombok.Data;
 3 
 4 /**
 5  * Created by ganbo on 2019/6/17.
 6  */
 7 
 8 @Data
 9 @AllArgsConstructor
10 public class User {
11     private Long id;
12     private String name;
13 
14     @Override
15     public boolean equals(final Object o) {
16         if (this == o) return true;
17         if (!(o instanceof User)) return false;
18         if (super.equals(o)) return true;
19 
20         final User user = (User) o;
21 
22         if (id != null ? !id.equals(user.id) : user.id != null) return false;
23         return name != null ? name.equals(user.name) : user.name == null;
24     }
25 
26     @Override
27     public int hashCode() {
28         int result = 1;
29         result = 31 * result + (id != null ? id.hashCode() : 0);
30         result = 31 * result + (name != null ? name.hashCode() : 0);
31         return result;
32     }
33 
}

The above code shows the rewritten User class equals and hashCode method, the following lines of code:

1     public static void main(String[] args) {
2         User u1 = new User(1L, "root");
3         User u2 = new User(1L, "root");
4 
5         System.out.println(u1.equals(u2));
6         System.out.println(u1.hashCode() == u2.hashCode());
7     }

At this time u1.equals (u2) certain returns true, if only rewriting without overwriting equals hashCode, then the User class default hashCode method hashCode method is inherited parent class Object, since the default hashCode method is based on the object memory address

After hash algorithm come, and obviously this time hashCode value u1 and u2 target object is not necessarily equal, this time the object as HashMap of key issues will appear inconsistent with expectations (two objects equals, but there's MashMap two groove).

However rewrite equals, and returns u1.equals (u2) true, according to the rules of hashcode, whose hash value is equal to two objects must equal, the contradiction arises, thus override equals hashcode must be rewritten, and from method override the hashcode class can be seen,

After rewriting the return of two new properties hash value User concerned.

The following are some of the provisions of hashcode:

Two equal objects, hashcode necessarily be equal

Two objects ranging, not necessarily ranging hashcode

hashcode equal, not necessarily equal two objects

hashcode ranging from two objects ranging from certain

 

Guess you like

Origin www.cnblogs.com/ganbo/p/11039053.html