package com.yanshu.utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jboss.jandex.Main;
import com.alibaba.fastjson.JSON;
public class MapUtil {
public static String ListTojson(List<HashMap> map2){
if(map2.size()==0){
return "{}";
}
Map<String,List> map=new HashMap<String,List>();
Set<String> set=map2.get(0).keySet();
for (String setchild : set) {
map.put(setchild, new ArrayList<>());
}
for (int i = 0; i < map2.size(); i++) {
for (String str : set) {
map.get(str).add(map2.get(i).get(str)==null?0:map2.get(i).get(str));
}
}
return JSON.toJSONString(map);
}
public static String ListTurnMap(List<HashMap> maps)
{
//如何长度等于0,返回空
if(maps.size()==0)
{
return "{}";
}
Map<String,List> map=new HashMap<>();
Set<String> set=maps.get(0).keySet();//获取List<HashMap> maps的第一个键
System.out.println("set--->>>"+set);
for (String setchild : set) {
map.put(setchild,new ArrayList<>());//[{tom=23, mary=12}, {tom=33, mary=22}]转换成{tom=[], mary=[]}
}
System.out.println("map-->>"+map);
//填值
for(int i=0;i<maps.size();i++)
{
for (String str : set) {
//map.get(str).add(map2.get(i).get(str)==null?0:map2.get(i).get(str));
map.get(str).add(maps.get(i).get(str)==null?0:maps.get(i).get(str));
}
}
return JSON.toJSONString(map);//{"tom":[23,33],"mary":[12,22]}
}
public static void main(String[] args) {
Map map1=new HashMap<>();
Map map2=new HashMap<>();
map1.put("tom",23);
map1.put("mary",12);
map2.put("tom",33);
map2.put("mary",22);
List list=new ArrayList();
list.add(map1);
list.add(map2);
System.out.println(list);
String listTojson = ListTurnMap(list);
System.out.println(listTojson);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jboss.jandex.Main;
import com.alibaba.fastjson.JSON;
public class MapUtil {
public static String ListTojson(List<HashMap> map2){
if(map2.size()==0){
return "{}";
}
Map<String,List> map=new HashMap<String,List>();
Set<String> set=map2.get(0).keySet();
for (String setchild : set) {
map.put(setchild, new ArrayList<>());
}
for (int i = 0; i < map2.size(); i++) {
for (String str : set) {
map.get(str).add(map2.get(i).get(str)==null?0:map2.get(i).get(str));
}
}
return JSON.toJSONString(map);
}
public static String ListTurnMap(List<HashMap> maps)
{
//如何长度等于0,返回空
if(maps.size()==0)
{
return "{}";
}
Map<String,List> map=new HashMap<>();
Set<String> set=maps.get(0).keySet();//获取List<HashMap> maps的第一个键
System.out.println("set--->>>"+set);
for (String setchild : set) {
map.put(setchild,new ArrayList<>());//[{tom=23, mary=12}, {tom=33, mary=22}]转换成{tom=[], mary=[]}
}
System.out.println("map-->>"+map);
//填值
for(int i=0;i<maps.size();i++)
{
for (String str : set) {
//map.get(str).add(map2.get(i).get(str)==null?0:map2.get(i).get(str));
map.get(str).add(maps.get(i).get(str)==null?0:maps.get(i).get(str));
}
}
return JSON.toJSONString(map);//{"tom":[23,33],"mary":[12,22]}
}
public static void main(String[] args) {
Map map1=new HashMap<>();
Map map2=new HashMap<>();
map1.put("tom",23);
map1.put("mary",12);
map2.put("tom",33);
map2.put("mary",22);
List list=new ArrayList();
list.add(map1);
list.add(map2);
System.out.println(list);
String listTojson = ListTurnMap(list);
System.out.println(listTojson);
}
}

本文介绍了一个实用的Java工具类,该类可以将List<HashMap>类型的集合转换为Map<String, List>类型的数据结构,便于进行进一步的数据处理和分析。文章通过具体示例展示了转换方法的实现细节。
991

被折叠的 条评论
为什么被折叠?



