Sobes.tech
Junior — Senior

Selection of unique Person by combination of first name and patronymic

livecode

Task condition

It is necessary to implement a function unique that takes a list of Person objects and returns a collection containing only unique entries. Uniqueness is determined by the pair of fields firstName and patronymicName; the values of id and secondName are ignored.

Requirements:

  • If the original list contains multiple objects with the same firstName and patronymicName, only one arbitrary instance should be kept.
  • The order of elements in the resulting collection is not mandatory.
  • To check for uniqueness, only compare the combination of firstName and patronymicName.
@Data
@AllArgsConstructor
public static class Person {

        public Integer id;
        public String firstName;
        public String secondName;
        public String patronymicName;
}

@Test
public void test() {
    List<Person> person = new ArrayList<>();
    person.add(new Person(1, "Ivan", "Vasya", "Petr"));
    person.add(new Person(2, "Ivan", "Vasya", "Petr"));
    person.add(new Person(3, "Ivan", "Vasya", "Petr"));
    person.add(new Person(4, "Ivan", "Gupta", "Ivan"));
    person.add(new Person(5, "Petr", "Rama", "Igor"));

    Collection<Person> uniquePerson = unique(person);
    assert uniquePerson.size() == 3;
}

public static Collection<Person> unique(List<Person> persons){

}