第一章 JDK8 API解析(1.1) MAP新增的merge方法介绍
2019-07-20 16:11
288 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lzc4869/article/details/96599788
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容器——HashMap及常用API,及put,get方法解析,哈希码的产生和使用
- JDK8新增便利的Map默认值方法
- JDK7新增Objects方法介绍(同时包含JDK8新添加的方法)
- JDK8 新增便利的 Map 默认值方法
- JDK8新增便利的Map默认值方法
- Java通过google map api实现地址解析的方法
- JDK8新增便利的Map默认值方法getOrDefault
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- 从头认识java-15.7 Map(2)-介绍HashMap的工作原理-put方法
- Map映射集合实现类HashMap的介绍和迭代遍历方法
- Map的实现类Hashtable各种方法源码解析
- map任务中,merge过程的实现方法
- RxJava中map方法的解析
- java中遍历map的几种方法介绍
- velocity显示List与Map的方法详细解析
- DownloadManager下载管理类2.3新增API介绍
- php版微信支付api.mch.weixin.qq.com域名解析慢原因与解决方法
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- google Map API实现地址解析