# equalsbuilder\&hashcodebuilder

[https://www.mkyong.com/java/java-how-to-overrides-equals-and-hashcode/](https://www.mkyong.com/java/java-how-to-overrides-equals-and-hashcode/)

## How to override equals and hashCode?

```java
import org.apache.commons.lang3.builder;

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

    //getters and setters, constructor

     @Override
    public boolean equals(Object o) {

        if (o == this) return true;
        if (!(o instanceof User)) {
            return false;
        }

        User user = (User) o;

        return new EqualsBuilder()
                .append(age, user.age)
                .append(name, user.name)
                .append(passport, user.passport)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(name)
                .append(age)
                .append(passport)
                .toHashCode();
    }

}
```
