[英]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)));
這可能嗎?
您可以僅將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}}
]
您的函數返回一個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)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.