Object equals and hashCode in Java

Object equal() and hashCode() in Java

The equals() and hashCode() methods are two of the most important methods in Java. They are used to compare objects for equality and to generate hash codes for objects.

The equals() method is defined in the Object class and is inherited by all Java objects. The default implementation of the equals() method compares the memory addresses of two objects. However, this is not always a reliable way to compare objects, as two objects can have the same memory address but different states.

For example, consider following code:

String s1 = new String("Hello");
String s2 = new String("Hello");

The Objects s1 and s2 have the same memory address, but they are not equal, as they refer to different instances of the String class.

To override the default implementation of the equals() method, you need to provide an implementation that compares the states of the two objects. For example:

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

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

    @Override
    public boolean equals(Object obj) 
    {
        if (obj == this) 
        {
            return true;
        }

        if (obj instanceof Person) 
        {
            Person other = (Person) obj;
            return name.equals(other.name) && age == other.age;
        }

        return false;
    }
}

The hashCode() method is also defined in the Object class and is inherited by all Java Objects. The default implementation of the hashCode() method simply returns the memory address of the Object. However, this is not always a good way to generate hashCodes, as two Objects with different states can have the same hashCode.

For example, consider the following code:

String s1 = new String("Hello");
String s2 = new String("Hello");

int hash1 = s1.hashCode();
int hash2 = s2.hashCode();

System.out.println(hash1); // 107225868
System.out.println(hash2); // 107225868

The objects s1 and s2 have the same hashCode, even though they are not equal.

To override the default implementation of the hashCode() method, you need to provide an implementation that generates a hash code that is consistent with the equals() method. For example:

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

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

    @Override
    public boolean equals(Object obj) 
    {
        if (obj == this) {
            return true;
        }

        if (obj instanceof Person) {
            Person other = (Person) obj;
            return name.equals(other.name) && age == other.age;
        }

        return false;
    }

    @Override
    public int hashCode() 
    {
        return Objects.hash(name, age);
    }
}

The Objects.hash() method is a utility method that generates a hash code for a given object. The hash code generated by Objects.hash() is consistent with the equals() method, so if two objects are equal, their hash codes will also be equal.

The equals() and hashCode() methods are important for a variety of reasons. They are used by collections to store and retrieve objects, and they are also used by hash functions to generate hash codes for objects.

When overriding the equals() and hashCode() methods, it is important to make sure that they are consistent with each other. If two objects are equal, their hash codes should also be equal. This ensures that objects are stored and retrieved correctly in collections, and it also ensures that hash functions work correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *