Java数据聚合问题
public class Demo {
public static void main(String[] args) throws IOException {
HashMap<Object, Object> m1 = new HashMap<>();
m1.put("month","2020-06");
m1.put("plan_working_hours",272.5);
m1.put("month_total_working_hours",120);
m1.put("project_code","ToB-APP");
m1.put("this_years_total_working_hours",287);
m1.put("project_name","B端APP框架");
m1.put("total_working_hours",307);
HashMap<Object, Object> m2 = new HashMap<>();
m2.put("month","2020-07");
m2.put("plan_working_hours",272.5);
m2.put("month_total_working_hours",93);
m2.put("project_code","ToB-APP");
m2.put("this_years_total_working_hours",287);
m2.put("project_name","B端APP框架");
m2.put("total_working_hours",307);
HashMap<Object, Object> m3 = new HashMap<>();
m3.put("month","2020-11");
m3.put("plan_working_hours",104);
m3.put("month_total_working_hours",16);
m3.put("project_code","1001");
m3.put("this_years_total_working_hours",67);
m3.put("project_name","C端底座");
m3.put("total_working_hours",150);
HashMap<Object, Object> m4 = new HashMap<>();
m4.put("month","2020-12");
m4.put("plan_working_hours",104);
m4.put("month_total_working_hours",51);
m4.put("project_code","1001");
m4.put("this_years_total_working_hours",67);
m4.put("project_name","C端底座");
m4.put("total_working_hours",150);
ArrayList<Map> maps = new ArrayList<>();
maps.add(m1);
maps.add(m2);
maps.add(m3);
maps.add(m4);
//todo 做数据聚合(
// 将project_name值相同的数据中的month和month_total_working_hours聚合到一个list中
// 每个project_name只有一个map数据)
/**
* 格式如下
*
* {
* "monthList": ["2020-06":120 ,"2020-07":93]
* "plan_working_hours": 272.5,
* "project_code": "ToB-APP",
* "this_years_total_working_hours": 287,
* "project_name": "B端APP框架",
* "total_working_hours": 307
* },
*/
}
}
### Map<String,Object> one = Maps.newHashMap();
one.put("one",1);
one.put("two",2);
Map<String,Object> two = Maps.newHashMap();
two.put("one",1);
two.put("two",2);
List<Map<String,Object>> list = Lists.newArrayList();
list.add(one);
list.add(two);
Set<String> keySet = one.keySet();
Map<String, List<Object>> result = list.stream().reduce(new HashMap<>(), (s, o) -> {
keySet.forEach(a -> {
if (s.containsKey(a)) {
List<Object> value = s.get(a);
value.add(o.get(a));
} else {
List<Object> value = Lists.newArrayList();
value.add(o.get(a));
s.put(a, value);
}
});
return s;
}, (s, o) -> s);
System.out.println(result);
###/*先根据project_name分组*/
Map<Object,java.util.List<Map>> result = maps.stream()
.collect(Collectors.groupingBy(d -> d.get("project_name").toString() ));
/*
* result返回示例
* {"B端APP框架":[{"month":"2020-06","plan_working_hours":272.5,"month_total_working_hours":120,"project_code":"ToB-APP","this_years_total_working_hours":287,"project_name":"B端APP框架","total_working_hours":307},{"month":"2020-07","plan_working_hours":272.5,"month_total_working_hours":93,"project_code":"ToB-APP","this_years_total_working_hours":287,"project_name":"B端APP框架","total_working_hours":307}],"C端底座":[{"month":"2020-11","plan_working_hours":104,"month_total_working_hours":16,"project_code":"1001","this_years_total_working_hours":67,"project_name":"C端底座","total_working_hours":150},{"month":"2020-12","plan_working_hours":104,"month_total_working_hours":51,"project_code":"1001","this_years_total_working_hours":67,"project_name":"C端底座","total_working_hours":150}]}
* */
/*用于最外层添加数据*/
JSONArray array = new JSONArray();
/*循环最外层, 也就是project_name这一层, 有几个project_name就循环几次*/
result.forEach((key,value)->{
JSONObject jsonObject = new JSONObject();
/*listMap其实就是你上面加到List的数据, 一共四条*/
List<Map> listMap = (List<Map>) value;
listMap.forEach(item->{
/*item就是一个一个map : project_code","ToB-APP" this_years_total_working_hours",287 .....*/
item.forEach((i,v) -> {
/*key值是month和month_total_working_hours根据要求特殊处理*/
if(StringUtils.equalsAny(i.toString(),"month","month_total_working_hours")){
Map<Object,Object> spa = new HashMap<>(16);
if(ObjectUtils.allNotNull(jsonObject.get("monthList"))){
spa = JSON.parseObject((String)jsonObject.get("monthList"),Map.class);
}
spa.put(i , v);
jsonObject.put("monthList", JSON.toJSONString(spa));
}
});
jsonObject.put("project_code",item.get("project_code"));
jsonObject.put("this_years_total_working_hours",item.get("this_years_total_working_hours"));
jsonObject.put("project_name",item.get("project_name"));
jsonObject.put("total_working_hours",item.get("total_working_hours"));
});
array.add(jsonObject);
} );
加了点注释 , 可能不是最优解,应该还有更优的解法。
JSON格式太乱 ,用JSON在线格式化格式化以后再看