I want to detect any duplicate object by its internal multiple properties, taken example:
class Person{
String name,
Integer age,
String address
//constructors
//getters setters
}
Now, out of above 3 parameters, I want to check duplication using 2 params, those are {name and age} I tried to achieve this using streams but seems there should be even simpler way using stream.
Current approach:
List<Person> personList = new ArrayList<>();
personList.add(new Person("name1", 10, "address1"));
personList.add(new Person("name2", 20, "address1"));
personList.add(new Person("name1", 10, "address2"));
personList.add(new Person("name3", 10, "address2"));
// Want to detect name1 and age 10 as a duplicate entry
Map<String, Map<Integer, List<Person>> nameAgePersonListMap = personList.stream()
.collect(Collectors.groupingBy(i -> i.getName(), Collectors.groupingBy(i -> i.getAge())));
// and later checking each element for size() > 1
Is there a further efficient way to determine duplicates in this use-case?