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