merge()
怎么用?程序员merge()
简介编程使用场景app
其余函数式编程
总结函数
Java 8最大的特性无异于更多地面向函数,有时约会了lambda
等,能够更好地进行函数式编程。
工具
前段时间无心间发现了map.merge()
方法,感受仍是很好用的,此文简单作一些相关介绍。首先咱们先看一个例子。网站
merge()
怎么用?
假设咱们有这么一段业务逻辑,我有一个学生成绩对象的列表,对象包含学生姓名,科目,科目分数三个属性,要求求得每一个学生的总成绩。ui
加入列表以下:this
private 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; }
咱们先看一下常规作法:spa
ObjectMapper objectMapper = new ObjectMapper(); List<StudentScore> studentScoreList = buildATestList(); Map<String, Integer> studentScoreMap = new HashMap<>(); studentScoreList.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)); // 结果以下: // {"李四":228,"张三":215,"王五":235}
而后再看一下merge()
是怎么作的:
Map<String, Integer> studentScoreMap2 = new HashMap<>(); studentScoreList.forEach(studentScore -> studentScoreMap2.merge( studentScore.getStuName(), studentScore.getScore(), Integer::sum)); System.out.println(objectMapper.writeValueAsString(studentScoreMap2)); // 结果以下: // {"李四":228,"张三":215,"王五":235}
merge()
简介
merge()
能够这么理解:不断新的值赋值到key(若是不存在)或更新给定的key值对应的值,其源码以下:
default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); Objects.requireNonNull(value); V oldValue = this.get(key); V newValue = oldValue == null ? value : remappingFunction.apply(oldValue, value); if (newValue == null) { this.remove(key); } else { this.put(key, newValue); } return newValue; }
咱们能够看到原理也是很简单的,该方法接收三个参数,一个键值,一个值,一个remappingFunction
,若是给定的键不存在,它就变成了put(key, value)
。
可是,若是key已经存在一些值,咱们remappingFunction
能够选择合并的方式,而后将合并获得的newValue
赋值给原先的key。
使用场景
这个使用场景相对来讲仍是比较多的,某种分组求和这类的操做,虽然stream中有相关groupingBy()
方法,可是若是你想在循环中作一些其余操做的时候,merge()
仍是一个挺不错的选择的。
其余
除了merge()
方法以外,我还看到了一些的Java 8中map
相关的其余方法,好比putIfAbsent
, ,compute()
,computeIfAbsent()
,computeIfPresent
这些方法咱们看名字应该就知道是什么意思了。
故此就不作过多介绍了,研究的能够简单阅读一下原始码(都仍是挺易懂的)。
这里咱们贴一下compute()(Map.class)
的源码,其返回值是计算后获得的新值:
default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { Objects.requireNonNull(remappingFunction); V oldValue = this.get(key); V newValue = remappingFunction.apply(key, oldValue); if (newValue == null) { if (oldValue == null && !this.containsKey(key)) { return null; } else { this.remove(key); return null; } } else { this.put(key, newValue); return newValue; } }
总结
本文简单介绍了一下Map.merge()
的方法,另外,Java 8中的HashMap
实现方法使用了TreeNode
和红黑树,在源码阅读上可能有一点缺点,不过原理上仍是类似的,compute()
同理。
IT技术分享社区
我的博客网站:https://programmerblog.xyz
文章推荐程序员效率:画流程图经常使用的工具程序员效率:整理经常使用的在线笔记软件远程办公:经常使用的远程协助软件,你都知道吗?51单片机程序下载、ISP及串口基础知识硬件:断路器、接触器、继电器基础知识