DEV Community

Cover image for Role of equals and hashCode Methods in Java HashMaps
Priyanshu Belwal
Priyanshu Belwal

Posted on

Role of equals and hashCode Methods in Java HashMaps

In Java, HashMap is a widely used data structure that stores elements in key-value pairs. It provides constant-time performance for basic operations like adding, removing, and accessing elements. The implementation of HashMap relies heavily on the equals and hashCode methods, which are used to ensure the uniqueness of keys and the efficiency of lookup operations. In this article, we'll dive deeper into how these methods are used in HashMap and what effect overriding them can have.

The equals() Method in HashMap:

The equals method is used to compare the equality of two objects. In the context of HashMap, it is used to compare the keys of the HashMap. When a key is added to the HashMap, the key's equals method is called to check if there is an existing key with the same value. If there is, the new value replaces the old one. If not, the key-value pair is added to the HashMap.

The default implementation of the equals method in Java compares the memory addresses of the two objects. However, in most cases, we want to compare the content of the objects instead. Therefore, it is often necessary to override the equals method in the key class to ensure that it compares the contents of the objects.

For example, let's say we have a Student class with name and id attributes. We want to use the id as the key in a HashMap, so we need to override the equals method to compare the ids instead of the memory addresses.

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

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

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id;
    }
}
Enter fullscreen mode Exit fullscreen mode

The hashCode() Method in HashMap:

The hashCode method returns a hash value that represents the object. In the context of HashMap, it is used to determine the index of the bucket where the key-value pair is stored. The default implementation of the hashCode method in Java returns the memory address of the object as an integer. However, this implementation is not suitable for most use cases, as it can result in poor performance and collisions.

To improve the performance of HashMap, we need to override the hashCode method in the key class to produce a more evenly distributed hash value. The general contract of the hashCode method is that if two objects are equal according to the equals method, they must have the same hash code. Therefore, it is important to ensure that the implementation of the hashCode method is consistent with the implementation of the equals method.

For example, in our Student class, we can override the hashCode method to return the hash code of the id attribute:

@Override
public int hashCode() {
    return Objects.hash(id);
}
Enter fullscreen mode Exit fullscreen mode

Effect of Overriding equals and hashCode Methods in HashMap:

When we override the equals and hashCode methods in the key class, we need to ensure that the implementation is consistent with the semantics of the class. If two objects are equal according to the equals method, they must have the same hash code. If two objects have the same hash code, they may or may not be equal according to the equals method.

If the implementation of the equals and hashCode methods is incorrect, it can result in unexpected behavior, such as incorrect lookup results or the loss of key-value pairs. Therefore, it is important to test the implementation of these methods thoroughly and ensure that they adhere to the contracts.

In conclusion, the equals and hashCode methods are essential components of the HashMap implementation

Top comments (0)