I've two objects in my java project that are something like this:
FavoriteGroup
- int id
- string userId
- string name
- List<Favorite> favorites
and
Favorite
- int id
- int groupId
- string title
- string path
Now, I've a List of items that may repeat the groups. Do I have a way to group that list in a way that for each FavoriteGroup I've all my Favorite list joined?
More graphical explanation: I've:
[{
id: 1,
userId: 1,
name: "Group one",
favorites: [{
id: 100,
groupId: 1,
title: "Favorite 1",
path: "path 1"
}]
},
{
id: 1,
userId: 1,
name: "Group one",
favorites: [{
id: 200,
groupId: 1,
title: "Favorite 2",
path: "path 2"
}]
},
{
id: 2,
userId: 1,
name: "Group two",
favorites: [{
id: 300,
groupId: 2,
title: "Favorite 3",
path: "path 3"
}]
}]
And I need:
[{
id: 1,
userId: 1,
name: "Group one",
favorites: [{
id: 100,
groupId: 1,
title: "Favorite 1",
path: "path 1"
},
{
id: 200,
groupId: 1,
title: "Favorite 2",
path: "path 2"
}]
},
{
id: 2,
userId: 1,
name: "Group two",
favorites: [{
id: 300,
groupId: 2,
title: "Favorite 3",
path: "path 3"
}]
}]
What's the best way to do this? using for loop iterations or maybe grouping java 8 stream functions?
Mapkeyed byidand decide whether a secondary group should update values or not, in case the multiple definitions of the same group has different values.