I have the following: List<Map<String, Object>>. I want to obtain a Map<String, List<Object> using streams. The key of this map will be the key, that it is repeated in the list. Basically, is a one-to-many, this would be an example of the data:
[
{ id: "a", name: "AAA"}
{ id: "a", name: "BBB"}
{ id: "b", name: "XXX"}
{ id: "b", name: "YYY"}
]
And I would like to obtain:
{
a: ["AAA", "BBB"],
b: ["XXX", "YYY"]
}
So, this new map is grouping by id from the previous list.
Mapdue to it would be simple the process.class MyObject{ String id; String name;}and use following code to achieve final result:list.stream() .collect(Collectors.groupingBy(MyObject::getId, Collectors.mapping(MyObject::getName, Collectors.toList())));