1. Introduction

As we all know, equals() and hashcode() are two very important methods in the Object class. Equals makes it easier to compare the similarities and differences between two objects. Hashcode gets the hashcode of the object so that we can find the object we want with 0(1) time complexity in some collection operations.

First, the default equals method is:

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

Returns true only if two objects refer to the same object (pointing to the same memory address). But most of the time we just want to compare whether two objects represent the same “value”, not whether two references refer to the same block of address. In this case, we need to override equals in our class to determine whether two objects are equal based on their property values. Such as:

public class Student {
    private int age;
    private String name;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        // Determine whether to refer to the same address
        if (obj==this)
            return true;
        // Check whether it is null or a different type
        if (obj==null|| obj.getClass()! =this.getClass())
            return false;
        // Convert the type
        Student student = (Student) obj;
        return student.age==this.age && (this.name==student.name || (this.name! =null && this.name.equals(student.name))); }}Copy the code

This allows us to determine whether two objects are equal by comparing certain attribute values of the two objects. However, we have not overwritten the hashcode method of the Student class.

2. Why is it necessary to override hashCode when overriding equals?

(1) If two objects checked by equals are true, then their hashcode calls must have the same hash value. (2) The hashcode of two objects judged false by equals may also have the same hash value. So why do you have to override HashCode when overriding equals?

We can understand that we have to rewrite HashCode if instances of our classes have any chance of being put into hashCode-based containers. Since in most cases we cannot assert that instances of this class will never be put into a hashMap, hashTable, etc., it is best to override the HashCode method when overriding equals.

why

Taking HashMap as an example, we know that get and PUT are very important methods in HashMap. When we want to put a key-value pair into a HashMap, we need to get the hashcode of the key, and specify the coordinate of the position to place the array through hashcode. If the hashcode (hashcode) of the key already exists in the array, the hashcode is checked by equals () of the key. If the equals () method is true, the two keys are the same, and the new value overwrites the old value. If equals returns false, the search continues in the list with the element as the head node until the same key is found or a new node is created at the end. If the hashCode method is not overridden, one of two things happens: (1) if you want to use two key values, which equals judgment is true, but not refer to the same object, then through the second key object’s hashcode to find the key of its value, both because not refer to the same address, hashcode may vary greatly, This means that the HashMap array has a different index. In this array, of course, there is no way to find equal keys through equals. This will result in two keys that are equal to each other. You can’t get a key from the HashMap. The result of get will be null. (2) If two keys are false using equals, their hashcodes will hold two keys in the same array subscript, but they will not be found using equals.