HashCode () = equals(); hashCode = equals(); hashCode = equals();
I think a lot of people will ask why, the so-called know the why, knowledge is not only know the conclusion but also know the cause.
The hashCode() method retrieves a hashCode and returns an int
For those of you who have studied data structures, the purpose of a hash code is to determine the index index of an object in a hash table. Hashsets and hashMaps, for example, use the hashCode method to determine index subscripts. If two objects return the same hashCode, it is called a “hash collision.”
Equals () checks whether two objects are equal. The equals() method is defined in the Object class. All classes have an Object parent, so if you do not override the equals() method, you will call the equals() method of the Object class.
The equals method of the Object class uses the “==” number to compare two objects. In many cases, the == number compares the memory address of two objects rather than the actual value, so it is not very suitable for business purposes. So many times we need to override equals to compare the value of each member variable in an object.
Why override hashCode () when overriding equals() compares whether two objects are equal?
If hashCode is equal, then equals() will be used to compare the equals() method. In other words, a HashSet and a HashMap determine whether two elements are equal by determining the hashCode first. If the two objects have different Hashcodes, they must not be equal.
Let’s do an experiment. We have a User class that just overrides equals() and puts it into the Set.
public class User {
private String id; private String name; private Integer age; public User(String id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() ! = o.getClass()) return false; User user = (User) o; return Objects.equals(id, user.id) && Objects.equals(name, user.name) && Objects.equals(age, user.age); } // Getter, setter, toString methodsCopy the code
} copy the code and then we loop through creating a User object with 10 member variables of the same value, and finally put it into the Set to duplicate.
public static void main(String[] args) { List list = new ArrayList<>(); for (int i = 0; i < 10; I++) {User User = new User(“1”, “2 “, 18); list.add(user); } Set set = new HashSet<>(list); for (User user : set) { System.out.println(user); } List users = list.stream().distinct().collect(Collectors.toList()); System.out.println(users); } we would expect the duplicate code to be de-duplicated, leaving only a “triple” user, but in fact, because the hashCode method was not overridden, it was not de-duplicated.
Then let’s rewrite some hashCode methods inside the User class and try again, nothing else.
Public class User {// Other unchanged
@override public int hashCode() {return objects.hash (id, name, age); }Copy the code
} Copy the code and run again, the result is correct.
The reason for this is that HashSet determines whether hashCode is equal or not. If hashCode is not equal, the two objects are considered not equal and will not be compared using equals(). Let’s look at hash codes that override the hashCode method and those that don’t.
This is the case without overwriting the hashCode method; each user object has a different hashCode, so a HashSet will be considered unequal.
This is the case when the hashCode method is overridden, because the hashCode is computed using the values of all of the object’s member variables, so as long as the member variables of both objects are equal, the resulting hashCode is the same.
So some people look at this and say, well, if two objects return the same hash code, are they necessarily equal?
The answer is not always true, because hashsets and hashmaps also use equals() to determine hash values.
In summary:
If the hash codes are not equal, the two objects must be different. Hash codes are equal. Two objects are not necessarily the same. If two objects are the same, the hash code and value must be equal. So to go back to the beginning, whenever you override equals, you must override hashCode. This is an important detail that can easily lead to business errors if you don’t pay attention to it. Ps need to self-study video attention B station: