preface

Recently review, saw this problem again, in this record and arrangement, through examples to illustrate the reason for this situation, so that you can clearly understand this problem.

Preliminary exploration

First we need to understand what the equals method is and what the HashCode method is.

The equals method

Equals is a method of Java obeJCT class. The equals source code is as follows:

public boolean equals(Object paramObject){
    return(this == paramObject);
}
Copy the code

From this we can see that equals is used to compare whether the memory addresses of two objects are equal.

HashCode methods

The hashCode method is the local method used to calculate a hash value of an object, which is the key to determining whether an object is duplicated in the collection.

A theorem

For equals objects, the hashCode must be the same.

Code sample

Create a Student class.

public class Student {

    private String name;
    private int age;
    private String QQ;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if(o == null || getClass() ! = o.getClass())return false;
        Student student = (Student) o;
        returnage == student.age && Objects.equals(name, student.name) && Objects.equals(QQ, student.QQ); }}Copy the code

In the Student class, we override the equals method.

Write a test class

public class Test {
    public static void main(String[] args) {
        Student student = new Student();
        Student student2 = new Student();
        System.out.println(student.equals(student2));    //true
        System.out.println(student.hashCode());            //356573597
        System.out.println(student2.hashCode());           //1735600054 
        HashMap<Student, String> map = new HashMap<>();
        map.put(student,"123");
        map.put(student2,"456"); System.out.println(map.get(student)); System.out.println(map.get(student2)); }}Copy the code

The output

true356573597 Student hashCode value 1735600054 Student 2 HashCode value 123 456Copy the code

In this case, we find that the equals objects are not equal, but the hashcodes are not equal, and the hash values calculated by the map are different, but the equals values are the same. This time meng meng. Exactly two objects are the same or not. So when we override equals, we have to override hashcode.

Redefine the student class

public class Student {

    private String name;
    private  int age;
    private  String QQ;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if(o == null || getClass() ! = o.getClass())return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name) &&
                Objects.equals(QQ, student.QQ);
    }

    @Override
    public int hashCode() {

        returnObjects.hash(name, age, QQ); }}Copy the code

The test again

public class Test {
    public static void main(String[] args) {
        Student student = new Student();
        Student student2 = new Student();
        System.out.println(student.equals(student2));   //true
        System.out.println(student.hashCode());          // 29791   
        System.out.println(student2.hashCode());       // 29791   
        HashMap<Student, String> map = new HashMap<>();
        map.put(student,"123");
        map.put(student2,"456");
        System.out.println(map.get(student));   //456
        System.out.println(map.get(student2)); //456

    }
}
Copy the code

Final output

true29791 // The same object 29791 456 // the description is stored with a value key, the same value 456Copy the code

Several theorems

If two objects are equal to each other, hashcode must be equal. 2. If two objects are not equal, hashcode may also be equal. 3. Hashcodes are equal, objects are not necessarily equal. 4, hashcode is not equal, the object must be different.