我正在尝试处理具有以下格式的列表:
List<Map<String, Map<String, Item>>
到Map<String, Map<String, List<Item>>
哪里Item
是一个包含两个属性的对象。
所以对于输入:
[
<"A1", <"B1", Item_1>>,
<"A1", <"B1", Item_2>>,
<"A1", <"B2", Item_3>>,
<"A2", <"B1", Item_4>>,
<"A2", <"B2", Item_5>>,
<"A2", <"B2", Item_6>>
]
输出应该是:
"A1" {
<"B1", [Item_1, Item_2]>
<"B2", [Item_3]>
}
"A2" {
<"B1", [Item_4]>
<"B2", [Item_5, Item_6]>
}
我尝试使用:
list.stream()
.flatMap(it -> it.entrySet().stream())
.collect(groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValues, Collectors.toList())));
但结果不是所需的,它返回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>
]
}
您能否建议我如何通过 B_ 键进行分组?谢谢!