1. 首页
  2. IT资讯

Java 8 中 Map 骚操作之 merge() 的用法

“u003Cimg src=”http:u002Fu002Fp1.pstatp.comu002Flargeu002Fpgc-imageu002FRf8n3jdBqecfy8″ img_width=”1080″ img_height=”345″ alt=”Java 8 中 Map 骚操作之 merge() 的用法” inline=”0″u003Eu003Cp class=”pgc-img-caption”u003E作者 | LQ木头u003Cu002Fpu003Eu003Cpu003Ejuejin.imu002Fpostu002F5d9b455ae51d45782b0c1bfbu003Cu002Fpu003Eu003Cpu003EJava 8 最大的特性无异于更多地面向函数,比如引入了 lambda等,可以更好地进行函数式编程。前段时间无意间发现了 map.merge 方法,感觉还是很好用的,此文简单做一些相关介绍。首先我们先看一个例子。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003Emerge 怎么用?u003Cu002Fh1u003Eu003Cpu003E假设我们有这么一段业务逻辑,我有一个学生成绩对象的列表,对象包含学生姓名、科目、科目分数三个属性,要求求得每个学生的总成绩。加入列表如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E private List<StudentScore> buildATestList {u003Cu002Fcodeu003Eu003Ccodeu003E L ist<StudentScore> studentScoreList = new ArrayList<>;u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore1 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“张三”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“语文”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(70);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore2 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“张三”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“数学”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(80);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore3 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“张三”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“英语”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(65);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore4 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“李四”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“语文”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(68);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore5 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“李四”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“数学”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(70);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore6 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“李四”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“英语”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(90);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore7 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“王五”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“语文”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(80);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore8 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“王五”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“数学”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(85);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Ccodeu003E StudentScore studentScore9 = new StudentScore {{u003Cu002Fcodeu003Eu003Ccodeu003E setStuName(“王五”);u003Cu002Fcodeu003Eu003Ccodeu003E setSubject(“英语”);u003Cu002Fcodeu003Eu003Ccodeu003E setScore(70);u003Cu002Fcodeu003Eu003Ccodeu003E }};u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E studentScoreList.add(studentScore1);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore2);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore3);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore4);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore5);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore6);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore7);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore8);u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.add(studentScore9);u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E return studentScoreList;u003Cu002Fcodeu003Eu003Ccodeu003E}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E我们先看一下常规做法:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E ObjectMu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003Eer objectMu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003Eer = new ObjectMu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003Eer;u003Cu002Fcodeu003Eu003Ccodeu003E List<StudentScore> studentScoreList = buildATestList;u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E Map<String, Integer> studentScoreMap = new HashMap<>;u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.forEach(studentScore -> {u003Cu002Fcodeu003Eu003Ccodeu003E if (studentScoreMap.containsKey(studentScore.getStuName)) {u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreMap.put(studentScore.getStuName, u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreMap.get(studentScore.getStuName) + studentScore.getScore);u003Cu002Fcodeu003Eu003Ccodeu003E } else {u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreMap.put(studentScore.getStuName, studentScore.getScore);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E });u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E System.out.println(objectMu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003Eer.writeValueAsString(studentScoreMap));u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003Eu002Fu002F 结果如下:u003Cu002Fcodeu003Eu003Ccodeu003Eu002Fu002F {“李四”:228,”张三”:215,”王五”:235}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E然后再看一下 merge 是怎么做的:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E Map<String, Integer> studentScoreMap2 = new HashMap<>;u003Cu002Fcodeu003Eu003Ccodeu003E studentScoreList.forEach(studentScore -> studentScoreMap2.merge(u003Cu002Fcodeu003Eu003Ccodeu003E studentScore.getStuName,u003Cu002Fcodeu003Eu003Ccodeu003E studentScore.getScore,u003Cu002Fcodeu003Eu003Ccodeu003E Integer::sum));u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E System.out.println(objectMu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003Eer.writeValueAsString(studentScoreMap2));u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003Eu002Fu002F 结果如下:u003Cu002Fcodeu003Eu003Ccodeu003Eu002Fu002F {“李四”:228,”张三”:215,”王五”:235}u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003Emerge 简介u003Cu002Fh1u003Eu003Cpu003Emerge 可以这么理解:它将新的值赋值到 key (如果不存在)或更新给定的key 值对应的 value,其源码如下:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction) {u003Cu002Fcodeu003Eu003Ccodeu003E Objects.requireNon(remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction);u003Cu002Fcodeu003Eu003Ccodeu003E Objects.requireNon(value);u003Cu002Fcodeu003Eu003Ccodeu003E V oldValue = this.get(key);u003Cu002Fcodeu003Eu003Ccodeu003E V newValue = oldValue == ? value : remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction.u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003Ely(oldValue, value);u003Cu002Fcodeu003Eu003Ccodeu003E if (newValue == ) {u003Cu002Fcodeu003Eu003Ccodeu003E this.remove(key);u003Cu002Fcodeu003Eu003Ccodeu003E } else {u003Cu002Fcodeu003Eu003Ccodeu003E this.put(key, newValue);u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cbru003Eu003Ccodeu003E return newValue;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003E我们可以看到原理也是很简单的,该方法接收三个参数,一个 key 值,一个 value,一个 remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction ,如果给定的key不存在,它就变成了 put(key, value) 。u003Cu002Fpu003Eu003Cpu003E但是,如果 key 已经存在一些值,我们 remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction 可以选择合并的方式,然后将合并得到的 newValue 赋值给原先的 key。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003E使用场景u003Cu002Fh1u003Eu003Cpu003E这个使用场景相对来说还是比较多的,比如分组求和这类的操作,虽然 stream 中有相关 groupingBy 方法,但如果你想在循环中做一些其他操作的时候,merge 还是一个挺不错的选择的。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003E其他u003Cu002Fh1u003Eu003Cpu003E除了 merge 方法之外,我还看到了一些Java 8 中 map 相关的其他方法,比如 putIfAbsent 、compute 、computeIfAbsent 、computeIfPresent,这些方法我们看名字应该就知道是什么意思了,故此处就不做过多介绍了,感兴趣的可以简单阅读一下源码(都还是挺易懂的),这里我们贴一下 compute(Map.class) 的源码,其返回值是计算后得到的新值:u003Cu002Fpu003Eu003Cpreu003Eu003Ccodeu003E default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction) {u003Cu002Fcodeu003Eu003Ccodeu003E Objects.requireNon(remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction);u003Cu002Fcodeu003Eu003Ccodeu003E V oldValue = this.get(key);u003Cu002Fcodeu003Eu003Ccodeu003E V newValue = remu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003EingFunction.u003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-3″u003Eappu003Cu002Fiu003Ely(key, oldValue);u003Cu002Fcodeu003Eu003Ccodeu003E if (newValue == ) {u003Cu002Fcodeu003Eu003Ccodeu003E if (oldValue == && !this.containsKey(key)) {u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E } else {u003Cu002Fcodeu003Eu003Ccodeu003E this.remove(key);u003Cu002Fcodeu003Eu003Ccodeu003E return ;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E } else {u003Cu002Fcodeu003Eu003Ccodeu003E this.put(key, newValue);u003Cu002Fcodeu003Eu003Ccodeu003E return newValue;u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Ccodeu003E }u003Cu002Fcodeu003Eu003Cu002Fpreu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003E总结u003Cu002Fh1u003Eu003Cpu003E本文简单介绍了一下 Map.merge 的方法,除此之外,Java 8 中的 HashMap 实现方法使用了 TreeNode 和 红黑树,在源码阅读上可能有一点难度,不过原理上还是相似的,compute 同理。所以,源码肯定是要看的,不懂的地方多读多练自然就理解了。u003Cu002Fpu003Eu003Cpu003Eu003Cu002Fpu003Eu003Ch1 toutiao-origin=”h2″u003E链接u003Cu002Fh1u003Eu003Cpu003E参考:u003Cu002Fpu003Eu003Cblockquoteu003Eu003Cpu003Ehttps:u002Fu002Fu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-4″u003Ewww.u003Cu002Fiu003Ejianshuu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Eu002Fpu002F68e6bu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E30u003Cu002Fiu003E410b0u003Cu002Fpu003Eu003Cu002Fblockquoteu003Eu003Cpu003E测试代码地址:u003Cu002Fpu003Eu003Cblockquoteu003Eu003Cpu003Ehttps:u002Fu002Fgithubu003Ci class=”chrome-extension-mutihighlight chrome-extension-mutihighlight-style-6″u003E.comu003Cu002Fiu003Eu002Flq920320u002Falgorithm-java-testu002Fblobu002Fmasteru002Fsrcu002Ftestu002Fjavau002Fotheru002FMapMethodsTest.javau003Cu002Fpu003Eu003Cu002Fblockquoteu003Eu003Cpu003E-END-u003Cu002Fpu003E”

原文始发于:Java 8 中 Map 骚操作之 merge() 的用法

主题测试文章,只做测试使用。发布者:℅傍ㄖ免沦陷dε鬼,转转请注明出处:http://www.cxybcw.com/17924.html

联系我们

13687733322

在线咨询:点击这里给我发消息

邮件:1877088071@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

QR code