java 将列表<Map>转换为地图<List>



我在List<Map>以下作为

List<Map<String,String>> list =
{"key1":"1234"}
{"key1":"3422"}
{"key1":"7565"}
{"key2":"foo"}
{"key2":"bar"}
{"key3":"xyz"}
{"key4":"pqr"}

我需要将其转换为Map<字符串,列表>作为

{"key1":"1234","3422","7565"}
{"key2":"foo","bar"}
{"key3":"xyz"}
{"key4":"pqr"}
Map<String, List<String> myMap = new HashMap();
List<String> tempList = new ArrayList();
for(Map<String, String> entry : myList){
if (myMap.contains(entry.getKey()))
tempList = myMap.getValue();
else
tempList = new ArrayList();

tempList.add(entry.getValue());
myMap.put(entry.getKey(), tempList);
}

最新更新