Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

Java数据聚合问题请教?

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
     *   },
     */
    
}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
        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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...