Java8 - 如何将嵌套映射转换为按内部映射键值收集的嵌套映射列表
Java8 - How to convert a nested maps to list of nested maps gathered by value of inner map's key
我有一个如下所示的嵌套地图对象
{12345={{"status":"200","outcome":"Success","message":"Account created"}}
{23121={{"status":"400","outcome":"Exception","message":"Invalid State value"}}
{43563={{"status":"200","outcome":"Success","message":"Account updated"}}
{72493={{"status":"400","outcome":"Exception","message":"Bad Request"}}
我需要将其转换为 Map<String, List<Map<String, Map<String, String>>>
,其中外部地图的键是状态值 ( 200 or 400 )
,该值是状态为 200 或 400 或其他的原始地图列表来自内部地图的有效值。
所以,新结构看起来像
{{200={[{12345={"status":"200","outcome":"Success","message":"Account created"}},
{43563={"status":"200","outcome":"Success","message":"Account updated"}}
]
},
{400={[{23121={"status":"400","outcome":"Exception","message":"Invalid State value"}},
{72493={"status":"400","outcome":"Exception","message":"Bad Request"}}
]
}
}
最终,我需要根据不同的统计数据生成报告。
这是我开始的,但卡住了。
我想遍历外部地图,获取内部地图,获取状态键的值并根据状态代码值将地图添加到列表中。
这就是我使用循环的方式
private static Map<String, List<Map<String, Map<String, String>>>> covertToReport(Map<String, Map<String, String>> originalMap) {
Map<String, List<Map<String, Map<String, String>>>> statusBasedListOfMaps = new TreeMap<>();
//loop through the map
//for each key, get the inner map
//get the status value for each inner map
List<Map<String, Map<String, String>>> accountsMapsList;
for (Entry<String, Map<String, String>> entry : originalMap.entrySet()) {
String accNum = entry.getKey();
Map<String, String> childMap = entry.getValue();
String stausVal = childMap.get("status");
accountsMapsList = statusBasedListOfMaps.get(stausVal) == null ? new ArrayList<>() : statusBasedListOfMaps.get(stausVal);
accountsMapsList.add((Map<String, Map<String, String>>) entry);
statusBasedListOfMaps.put(stausVal, accountsMapsList);
}
return statusBasedListOfMaps;
}
当然,下面的代码无法编译,但这就是我想要得到的。
private static void covertToReport(Map<String, Map<String, String>> originalMap) {
Map<String, List<Map<String, Map<String, String>>>> statusBasedListOfMaps;
statusBasedListOfMaps = originalMap.entrySet()
.stream()
.filter(e -> e.getValue()
.values()
.stream()
.map(innerMap -> Collectors.toList())
.collect(Collectors.toMap(Map.Entry::getKey, Collectors.toList(e)));
这可能吗?
你的函数return一个Map<String, List<Map<String, Map<String, String>>>>
,但是你的结构看起来像Map<String, Map<String, Map<String, String>>>
如果您真正喜欢的是 Map<String, Map<String, Map<String, String>>>
代码如下:
Map<String, Map<String, Map<String, String>>> result= map.entrySet().stream().collect(Collectors.groupingBy(entry -> entry.getValue().get("status"), Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
你可以只使用 Collectors.groupingBy()
和 Collectors.mapping()
:
private static Map<String, List<Map<String, Map<String, String>>>> convertToReport(Map<String, Map<String, String>> originalMap) {
return originalMap.entrySet().stream()
.collect(Collectors.groupingBy(e -> e.getValue().get("status"),
Collectors.mapping(Map::ofEntries, Collectors.toList())));
}
您按 status
分组,然后使用 Map.ofEntries()
将关联的条目映射到自己的地图。如果你使用 Java 你可以用这个代替 Map::ofEntries
:
e -> new HashMap<>() {{ put(e.getKey(), e.getValue()); }}
结果将是这样的:
200=[
{12345={status=200, message=Account created, outcome=Success}},
{43563={status=200, message=Account created, outcome=Success}}
],
400=[
{72493={status=400, message=Invalid State value, outcome=Exception}},
{23121={status=400, message=Invalid State value, outcome=Exception}}
]
我有一个如下所示的嵌套地图对象
{12345={{"status":"200","outcome":"Success","message":"Account created"}}
{23121={{"status":"400","outcome":"Exception","message":"Invalid State value"}}
{43563={{"status":"200","outcome":"Success","message":"Account updated"}}
{72493={{"status":"400","outcome":"Exception","message":"Bad Request"}}
我需要将其转换为 Map<String, List<Map<String, Map<String, String>>>
,其中外部地图的键是状态值 ( 200 or 400 )
,该值是状态为 200 或 400 或其他的原始地图列表来自内部地图的有效值。
所以,新结构看起来像
{{200={[{12345={"status":"200","outcome":"Success","message":"Account created"}},
{43563={"status":"200","outcome":"Success","message":"Account updated"}}
]
},
{400={[{23121={"status":"400","outcome":"Exception","message":"Invalid State value"}},
{72493={"status":"400","outcome":"Exception","message":"Bad Request"}}
]
}
}
最终,我需要根据不同的统计数据生成报告。
这是我开始的,但卡住了。
我想遍历外部地图,获取内部地图,获取状态键的值并根据状态代码值将地图添加到列表中。
这就是我使用循环的方式
private static Map<String, List<Map<String, Map<String, String>>>> covertToReport(Map<String, Map<String, String>> originalMap) {
Map<String, List<Map<String, Map<String, String>>>> statusBasedListOfMaps = new TreeMap<>();
//loop through the map
//for each key, get the inner map
//get the status value for each inner map
List<Map<String, Map<String, String>>> accountsMapsList;
for (Entry<String, Map<String, String>> entry : originalMap.entrySet()) {
String accNum = entry.getKey();
Map<String, String> childMap = entry.getValue();
String stausVal = childMap.get("status");
accountsMapsList = statusBasedListOfMaps.get(stausVal) == null ? new ArrayList<>() : statusBasedListOfMaps.get(stausVal);
accountsMapsList.add((Map<String, Map<String, String>>) entry);
statusBasedListOfMaps.put(stausVal, accountsMapsList);
}
return statusBasedListOfMaps;
}
当然,下面的代码无法编译,但这就是我想要得到的。
private static void covertToReport(Map<String, Map<String, String>> originalMap) {
Map<String, List<Map<String, Map<String, String>>>> statusBasedListOfMaps;
statusBasedListOfMaps = originalMap.entrySet()
.stream()
.filter(e -> e.getValue()
.values()
.stream()
.map(innerMap -> Collectors.toList())
.collect(Collectors.toMap(Map.Entry::getKey, Collectors.toList(e)));
这可能吗?
你的函数return一个Map<String, List<Map<String, Map<String, String>>>>
,但是你的结构看起来像Map<String, Map<String, Map<String, String>>>
如果您真正喜欢的是 Map<String, Map<String, Map<String, String>>>
代码如下:
Map<String, Map<String, Map<String, String>>> result= map.entrySet().stream().collect(Collectors.groupingBy(entry -> entry.getValue().get("status"), Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
你可以只使用 Collectors.groupingBy()
和 Collectors.mapping()
:
private static Map<String, List<Map<String, Map<String, String>>>> convertToReport(Map<String, Map<String, String>> originalMap) {
return originalMap.entrySet().stream()
.collect(Collectors.groupingBy(e -> e.getValue().get("status"),
Collectors.mapping(Map::ofEntries, Collectors.toList())));
}
您按 status
分组,然后使用 Map.ofEntries()
将关联的条目映射到自己的地图。如果你使用 Java 你可以用这个代替 Map::ofEntries
:
e -> new HashMap<>() {{ put(e.getKey(), e.getValue()); }}
结果将是这样的:
200=[
{12345={status=200, message=Account created, outcome=Success}},
{43563={status=200, message=Account created, outcome=Success}}
],
400=[
{72493={status=400, message=Invalid State value, outcome=Exception}},
{23121={status=400, message=Invalid State value, outcome=Exception}}
]