I am trying to process a List which has the following format:
List<Map<String, Map<String, Item>>
to Map<String, Map<String, List<Item>>
where Item
is an object which contains two attributes.
So for the input:
[
<"A1", <"B1", Item_1>>,
<"A1", <"B1", Item_2>>,
<"A1", <"B2", Item_3>>,
<"A2", <"B1", Item_4>>,
<"A2", <"B2", Item_5>>,
<"A2", <"B2", Item_6>>
]
The output should be:
"A1" {
<"B1", [Item_1, Item_2]>
<"B2", [Item_3]>
}
"A2" {
<"B1", [Item_4]>
<"B2", [Item_5, Item_6]>
}
I tried by using:
list.stream()
.flatMap(it -> it.entrySet().stream())
.collect(groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValues, Collectors.toList())));
but the result is not the desired one, it returns Map<String, List<Map<String, Item>>>
{
"A1": [
<"B1", Item_1>,
<"B1", Item_2>,
<"B2", Item_3>
]
"A2": [
<"B1", Item_4>,
<"B2", Item_5>,
<"B2", Item_6>
]
}
Can you please advice how can I proceed by grouping also by B_ keys? Thank you!