java 将列表<Map> 转换为地图<List>
java convert List<Map> to Map<List>
我低于List<Map>
如下
List<Map<String,String>> list =
{"key1":"1234"}
{"key1":"3422"}
{"key1":"7565"}
{"key2":"foo"}
{"key2":"bar"}
{"key3":"xyz"}
{"key4":"pqr"}
我需要将其转换为 Map as
{"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);
}
我低于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);
}