ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[转]jdk8中map新增的merge方法

2021-12-02 18:01:49  阅读:143  来源: 互联网

标签:map StudentScore list merge add jdk8 new


1 前言

转摘于:jdk8中map新增的merge方法介绍

2 内容

1.Map.merge方法介绍

  jdk8对于许多常用的类都扩展了一些面向函数,lambda表达式,方法引用的功能,使得java面向函数编程更为方便。其中Map.merge方法就是其中一个,merge方法有三个参数,key:map中的键,value:使用者传入的值,remappingFunction:BiFunction函数接口(该接口接收两个值,执行自定义功能并返回最终值)。当map中不存在指定的key时,便将传入的value设置为key的值,当key存在值时,执行一个方法该方法接收key的旧值和传入的value,执行自定义的方法返回最终结果设置为key的值。

//map.merge方法源码default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if(newValue == null) {
remove(key);
} else {
put(key, newValue);
}
return newValue;
}

 比如以下代码的含义:当name不存在时设置name的值为1,当name的值存在时,将值加1赋给name

public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("name", 1);
map.merge("name", 1, (oldValue, newValue) -> oldValue + newValue);
map.merge("count", 1, (oldValue, newValue) -> oldValue + newValue);
System.out.println(map);
}
//返回结果//{count=1, name=2}2.map.merge()方法使用场景 merge方法在统计时用的场景比较多,这里举一个统计学生总成绩的例子来说明。现在有一个学生各科成绩的集合,要统计每个学生的总成绩,以下给出使用merge方法与不使用的写法 public class StudentScoreSum { @Data static class StudentScore { private Integer sid; private String scoreName; private Integer score; public StudentScore(Integer sid, String scoreName, Integer score) { this.sid = sid; this.scoreName = scoreName; this.score = score; } public StudentScore() { } } public static void main(String[] args) { List<StudentScore> list = new ArrayList<>(); list.add(new StudentScore(1, "chinese", 110)); list.add(new StudentScore(1, "english", 120)); list.add(new StudentScore(1, "math", 135)); list.add(new StudentScore(2, "chinese", 99)); list.add(new StudentScore(2, "english", 100)); list.add(new StudentScore(2, "math", 133)); list.add(new StudentScore(3, "chinese", 88)); list.add(new StudentScore(3, "english", 140)); list.add(new StudentScore(3, "math", 90)); list.add(new StudentScore(4, "chinese", 108)); list.add(new StudentScore(4, "english", 123)); list.add(new StudentScore(4, "math", 114)); list.add(new StudentScore(5, "chinese", 116)); list.add(new StudentScore(5, "english", 133)); list.add(new StudentScore(5, "math", 135)); System.out.println(sum1(list)); System.out.println(sum2(list)); } //传统写法 public static Map<Integer, Integer> sum1(List<StudentScore> list) { Map<Integer, Integer> map = new HashMap<>(); for (StudentScore studentScore : list) { if (map.containsKey(studentScore.getSid())) { map.put(studentScore.getSid(), map.get(studentScore.getSid()) + studentScore.getScore()); } else { map.put(studentScore.getSid(), studentScore.getScore()); } } return map; } //merger写法 public static Map<Integer, Integer> sum2(List<StudentScore> list) { Map<Integer, Integer> map = new HashMap<>(); list.stream().forEach(studentScore -> map.merge(studentScore.getSid() , studentScore.getScore(), Integer::sum)); return map; } }//输出结果 {1=365, 2=332, 3=318, 4=345, 5=384} {1=365, 2=332, 3=318, 4=345, 5=384}

3 总结

  merger方法使用起来确实在一定程度上减少了代码量,使得代码更加简洁。可见,java8新增的函数是编程确实能让我们少些点模板代码,更加关注与业务实现。

标签:map,StudentScore,list,merge,add,jdk8,new
来源: https://www.cnblogs.com/fanbi/p/15634499.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有