2

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.

1
  • 1
    IMO use object instead of Map due 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()))); Commented Apr 1, 2019 at 16:23

1 Answer 1

4

Use Collectors.groupingBy along with Collectors.mapping:

Map<String, List<Object>> result = listOfMaps.stream()
    .collect(Collectors.groupingBy(map -> map.get("id").toString(),
                                   Collectors.mapping(map -> map.get("name"),
                                                      Collectors.toList())));

Collectors.groupingBy creates the map you want, while Collectors.mapping adapts a collector into another one by applying a mapping function to each element of the stream. (In this case, it collects to a list by first transforming each map of the stream to the value mapped to the "name" key).

If needed, the code above might be modified to skip maps that don't have an entry with the "id" key:

Map<String, List<Object>> result = listOfMaps.stream()
    .filter(map -> map.get("id") != null))
    .collect(Collectors.groupingBy(map -> map.get("id").toString(),
                                   Collectors.mapping(map -> map.get("name"),
                                                      Collectors.toList())));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.