Skip to content

Files

Latest commit

 

History

History
66 lines (50 loc) · 1.57 KB

EqualsHashCode.md

File metadata and controls

66 lines (50 loc) · 1.57 KB

Pattern: Missing override for equals() or hashCode()

Issue: -

Description

The contract of equals() and hashCode() requires that equal objects have the same hashCode. Therefore, whenever you override equals() you must override hashCode() to ensure that your class can be used in hash-based collections.

Default configuration

<module name="EqualsHashCode"/>

Examples

Example of incorrect code:

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

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + name.hashCode();
        result = 31 * result + age;
        return result;
    }
}

Example of correct code:

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

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + name.hashCode();
        result = 31 * result + age;
        return result;
    }
    
    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof User)) {
            return false;
        }

        User user = (User) o;
        return user.name.equals(name) && user.age == age;
    }
}

Further Reading