I have a custom type which looks like this:
public class SampleObject {
private UUID id;
private List<ValueObject> values;
// getters, all-args constructor, etc.
}
The ValueObject just contains some Strings.
And I have a List of SampleObjects payload. Within that list, there are several objects with the same id but different values in the List<ValueObject>.
What I want to archive is to merge all objects with the same id and add all the different lists of the ValueObjects in one merged object.
At the end, I need again a list of SampleObjects.
What I tried is the following:
List<SampleObject> payload = // initializing the List
payload.stream().collect(Collectors.groupingBy(SampleObject::getId));
But this returns a Map of List of SampleObjects.
And I don't know how to merge then the objects under the value of this Map.
SampleObjectspayloadand you want to collectValueObjects that are contained in the lists corresponding to the sameidinto a singleList, and create a newSampleObjectbased on each aggregatedList, right? And the result should be of typeList<SampleObject>?SampleObject.