List<Map<String, Object>> map = new ArrayList<Map<String,Object>>();
Map<String, Object> mapList = new HashMap<>();
mapList.put("xxx", 1);
Map<String, Object> mapList2 = new HashMap<>();
mapList2.put("xxx", 2);
Map<String, Object> mapList3 = new HashMap<>();
mapList3.put("xxx", 3);
map.add(mapList2);
map.add(mapList3);
map.add(mapList3);
转为Map<String,List<String>> 键是xxx,值是1,2,3
* list转map
* <p>
* Map<String,List<String>>
*/
private Map<String, List<String>> convert(List<Map<String, Object>> list) {
// 这是要返回的map对象
HashMap<String, List<String>> map = new HashMap<>();
// 对应的list
ArrayList<String> mapList = new ArrayList<>();
String key = null;
// 遍历list
for (Map<String, Object> item : list) {
// 取出item中所有对应的value
for (String itemKey : item.keySet()) {
// 因为key都是xxx,所以直接去第一个map的可以就可以了
if (key == null) {
key = itemKey;
}
mapList.add(String.valueOf(item.get(itemKey)));
}
}
// 遍历结束
map.put(key, mapList);
return map;
}
这个其实不难,主要是看map的keyset等api的用法
这种都是后台来实现的啊, 比如你想要查询的结果为map 你相应的配置文件里面返回值就是 这里resultType 定义了就返回是map了
mysql中,结果集转成json格式?
骚年,mysql没你想的那么强大,最好还是在java程序里转成json
List<Map<String, Object>> map = new ArrayList<Map<String,Object>>();
Map<String, Object> mapList = new HashMap<String, Object>();
mapList.put("xxx", 1);
Map<String, Object> mapList2 = new HashMap<String, Object>();
mapList2.put("xxx", 2);
Map<String, Object> mapList3 = new HashMap<String, Object>();
mapList3.put("xxx", 3);
map.add(mapList);
map.add(mapList2);
map.add(mapList3);
Map<String,List<String>> mapList1=new HashMap<String, List<String>>();
List<String> listStr=new ArrayList<String>();
for (int i = 0; i < map.size(); i++) {
listStr.add(map.get(i).get("xxx").toString());
}
mapList1.put("xxx", listStr);
System.out.println(mapList1.get("xxx"));
map.put("xxx",list)这个就可以了