Always override hashCode() when you override equals() to maintain the contract between them.
Source Code
public class Person {
private String name;
private int age;
// Constructor, getters and setters
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
return age == person.age && name.equals(person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}