Sobes.tech
Junior — Senior

Why does HashSet not recognize an object that has already been added

livecode

Task condition

In the example, an instance of the Person class is created and added to a HashSet, but calling the contains() method for the same object returns false. What could be the reason for this behavior?

public void personTask() {}
    Set<Person> people = new HashSet<>();
    Person person = new Person("Petrov", "Petr", "Petrovich");
    people.add(person);
    people.contains(person); // returns false

@Data
@AllArgsConstructor
private static class Person {
    private String firstName;
    private String lastName;
    private String middleName;
}