Lambda实现List Map 2列分组和求平均值 倒序排序 遍历

avatar 2020年12月22日22:40:10 4 219 views

下午一个朋友要求帮忙写个demo,大意就是 将List<Map<String, Object>> 根据2列分组然后求平均值,然后倒序排序 遍历的故事

直接上代码了

import java.util.*;
import java.util.stream.Collectors;

public class Result {

    /***
     *  题 目:将学生成绩按班级计算每一门课的平均分。
     *  入参:
     *  参见studentScoreList
     *  需求:
     *  1、得到班级课程平均分的一个list,并打印出来。包含字段:班级名称、课程、平均分。
     *  2、按平均分降序排列。
     */
    public static void main(String[] args) {

        List<Map<String, Object>> studentScoreList = getStudentScores();

        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, List<Map<String, Object>>> glist = studentScoreList.stream().collect(Collectors.groupingBy(e -> e.get("班级").toString() + "#" + e.get("课程名称").toString()));
        glist.forEach((k, slist) -> {
            Map<String, Object> nmap = new HashMap<>();
            nmap.put("班级", slist.get(0).get("班级"));
            nmap.put("课程名称", slist.get(0).get("课程名称"));

            // 求平均分
            IntSummaryStatistics statistics = (IntSummaryStatistics) slist.stream().collect(Collectors.summarizingInt(e -> Integer.valueOf(((Map) e).get("成绩").toString())));
            nmap.put("平均分", statistics.getAverage());//求和
            result.add(nmap);
        });

        //下面这块是原生写法
//        // 分组
//        for (Map<String, Object> map : studentScoreList) {
//            String key = map.get("班级") + "#" + map.get("课程名称");
//            if (glist.get(key) == null) {
//                List<Map<String, Object>> list = new ArrayList<>();
//                glist.put(key, list);
//            }
//            glist.get(key).add(map);
//        }
//
//        // 求平均分
//        for (Map.Entry<String, List<Map<String, Object>>> entry : glist.entrySet()) {
//            String key = entry.getKey();
//            List<Map<String, Object>> list = entry.getValue();
//            if (list.size() > 0) {
//                int sum = 0;
//                for (Map<String, Object> map : list) {
//                    sum += Integer.valueOf(map.get("成绩").toString());
//                }
//                double avg = sum * 1.0 / list.size();
//                Map<String, Object> tempMap = new HashMap<>();
//                tempMap.put("班级", key.split("#")[0]);
//                tempMap.put("课程名称", key.split("#")[1]);
//                tempMap.put("平均分", avg);
//                result.add(tempMap);
//            }
//        }

        // 把result排序
        Collections.sort(result, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                double d1 = Double.valueOf(o1.get("平均分").toString());
                double d2 = Double.valueOf(o2.get("平均分").toString());
                Integer num1 = (int) d1;
                Integer num2 = (int) d2;
                return num2.compareTo(num1);
            }
        });

        // 遍历result
        result.forEach(p -> {
            System.out.println(p.get("班级") + "\t" + p.get("课程名称") + "\t" + p.get("平均分"));
        });


    }

    private static List<Map<String, Object>> getStudentScores() {
        List<Map<String, Object>> studentScoreList = new ArrayList<Map<String, Object>>();
        Map<String, Object> studentScore1 = new HashMap<String, Object>();
        studentScore1.put("班级", "一班");
        studentScore1.put("课程名称", "语文");
        studentScore1.put("姓名", "王一");
        studentScore1.put("成绩", "85");
        studentScoreList.add(studentScore1);

        Map<String, Object> studentScore2 = new HashMap<String, Object>();
        studentScore2.put("班级", "一班");
        studentScore2.put("课程名称", "数学");
        studentScore2.put("姓名", "王一");
        studentScore2.put("成绩", "89");
        studentScoreList.add(studentScore2);

        Map<String, Object> studentScore3 = new HashMap<String, Object>();
        studentScore3.put("班级", "一班");
        studentScore3.put("课程名称", "语文");
        studentScore3.put("姓名", "王二");
        studentScore3.put("成绩", "78");
        studentScoreList.add(studentScore3);

        Map<String, Object> studentScore4 = new HashMap<String, Object>();
        studentScore4.put("班级", "一班");
        studentScore4.put("课程名称", "数学");
        studentScore4.put("姓名", "王二");
        studentScore4.put("成绩", "98");
        studentScoreList.add(studentScore4);
        Map<String, Object> studentScore11 = new HashMap<String, Object>();
        studentScore11.put("班级", "二班");
        studentScore11.put("课程名称", "语文");
        studentScore11.put("姓名", "王三");
        studentScore11.put("成绩", "74");
        studentScoreList.add(studentScore11);

        Map<String, Object> studentScore12 = new HashMap<String, Object>();
        studentScore12.put("班级", "二班");
        studentScore12.put("课程名称", "数学");
        studentScore12.put("姓名", "王三");
        studentScore12.put("成绩", "97");
        studentScoreList.add(studentScore12);

        Map<String, Object> studentScore13 = new HashMap<String, Object>();
        studentScore13.put("班级", "二班");
        studentScore13.put("课程名称", "语文");
        studentScore13.put("姓名", "王四");
        studentScore13.put("成绩", "77");
        studentScoreList.add(studentScore13);

        Map<String, Object> studentScore14 = new HashMap<String, Object>();
        studentScore14.put("班级", "二班");
        studentScore14.put("课程名称", "数学");
        studentScore14.put("姓名", "王四");
        studentScore14.put("成绩", "83");
        studentScoreList.add(studentScore14);

        return studentScoreList;
    }


}

  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
可以匿名评论或者登录后台评论,评论回复后会有邮件通知

  

已通过评论:2   待审核评论数:0
  1. avatar 言曌

    评论测试

  2. avatar 张三

    评论测试