Given a map of Person and Dog:
LinkedHashMap<Person, Dog> personDogMap = new LinkedHashMap<>();
The classes for Person and Dog:
class Person {
String name;
String id;
int age;
//getters
}
class Dog {
String name;
int age;
//getters
}
How can I get a Map<Person, List> extracting the duplicate values from this personDogMap
Scenario:
personDogMap.put(new Person("John", "12345", 23), new Dog("Luke", 5));
personDogMap.put(new Person("John", "12345", 23), new Dog("Lisa", 7));
personDogMap.put(new Person("John", "54323", 33), new Dog("Frank", 2));
personDogMap.put(new Person("Louis", "95223", 23), new Dog("Nick", 12));
In this case the output would be:
{
Person(name=John,
id=12345,
age=23)=[
Dog(name=Luke,
age=5),
Dog(name=Lisa,
age=7)
],
Person(name=John,
id=54323,
age=33)=[
Dog(name=Frank,
age=2)
],
Person(name=Louis,
id=95223,
age=23)=[
Dog(name=Nick,
age=12)
]
}
My first try was to: 1 - Check if there's duplicate values in the keyset(the id parameter it's the differentiator) 2 - If so, take this duplicate and add all the values from the duplicate key to the List
But I'm struggling to reduce the duplicate values. Any ideas?