• java 8 Map 之merge用法


    直接上代码

    1.学生成绩类

    public class StudentScore {
    
        private String stuName;
        private String subject;
        private int score;
    
        public String getStuName() {
            return stuName;
        }
    
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
    
        public String getSubject() {
            return subject;
        }
    
        public void setSubject(String subject) {
            this.subject = subject;
        }
    
        public int getScore() {
            return score;
        }
    
        public void setScore(int score) {
            this.score = score;
        }
    }
    

      2.计算总分

    /**
     * 要求求得每个学生的总成绩
     */
    public class MapDemo {
    
    
        public static void main(String[] args) throws JsonProcessingException {
    
            //获取准备数据
            List<StudentScore> list = buildATestList();
    
            //传统方法
            long start = System.currentTimeMillis();
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Integer> studentScoreMap = new HashMap<>();
            list.forEach(studentScore -> {
                if (studentScoreMap.containsKey(studentScore.getStuName())) {
                    studentScoreMap.put(studentScore.getStuName(),
                            studentScoreMap.get(studentScore.getStuName()) + studentScore.getScore());
                } else {
                    studentScoreMap.put(studentScore.getStuName(), studentScore.getScore());
                }
            });
    
            System.out.println(objectMapper.writeValueAsString(studentScoreMap));
            long end = System.currentTimeMillis();
            System.out.println(end - start);
    
            //新方法
            long start2 = System.currentTimeMillis();
            Map<String, Integer> studentScoreMap2 = new HashMap<>();
    
            list.forEach(studentScore -> studentScoreMap2.merge(
                    studentScore.getStuName(),
                    studentScore.getScore(),
                    Integer::sum));
    
            System.out.println(objectMapper.writeValueAsString(studentScoreMap2));
            long end2 = System.currentTimeMillis();
            System.out.println(end2 - start2);
        }
    
    
        //准备数据
        private static List<StudentScore> buildATestList() {
            List<StudentScore> studentScoreList = new ArrayList<>();
            StudentScore studentScore1 = new StudentScore() {{
                setStuName("张三");
                setSubject("语文");
                setScore(70);
            }};
            StudentScore studentScore2 = new StudentScore() {{
                setStuName("张三");
                setSubject("数学");
                setScore(80);
            }};
            StudentScore studentScore3 = new StudentScore() {{
                setStuName("张三");
                setSubject("英语");
                setScore(65);
            }};
            StudentScore studentScore4 = new StudentScore() {{
                setStuName("李四");
                setSubject("语文");
                setScore(68);
            }};
            StudentScore studentScore5 = new StudentScore() {{
                setStuName("李四");
                setSubject("数学");
                setScore(70);
            }};
            StudentScore studentScore6 = new StudentScore() {{
                setStuName("李四");
                setSubject("英语");
                setScore(90);
            }};
            StudentScore studentScore7 = new StudentScore() {{
                setStuName("王五");
                setSubject("语文");
                setScore(80);
            }};
            StudentScore studentScore8 = new StudentScore() {{
                setStuName("王五");
                setSubject("数学");
                setScore(85);
            }};
            StudentScore studentScore9 = new StudentScore() {{
                setStuName("王五");
                setSubject("英语");
                setScore(70);
            }};
    
            studentScoreList.add(studentScore1);
            studentScoreList.add(studentScore2);
            studentScoreList.add(studentScore3);
            studentScoreList.add(studentScore4);
            studentScoreList.add(studentScore5);
            studentScoreList.add(studentScore6);
            studentScoreList.add(studentScore7);
            studentScoreList.add(studentScore8);
            studentScoreList.add(studentScore9);
    
            return studentScoreList;
        }
    }

    3.最后结果

    {"李四":228,"张三":215,"王五":235}
    410
    {"李四":228,"张三":215,"王五":235}
    8
  • 相关阅读:
    【前端】用 npm 安装 yarn
    【前端】HTML复习巩固
    【前端】JS-删除绑定事件
    【前端】CSS3--动画animation的基本使用,3分钟快速实现一个小动画
    【GitHub】一文入门GitHub的使用,抓紧区交友吧!!
    【git】一文让你入门git的使用
    【前端】外边距合并问题 -- 嵌套盒子
    【前端】height 和 line-height的区别
    前端各种高度宽度视图
    关于 super() , 和 this , bind(this)的粗俗理解
  • 原文地址:https://www.cnblogs.com/ysyy/p/15318865.html
Copyright © 2020-2023  润新知