色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java 8 中 Map 騷操作之 merge() 的使用方法

瀏覽:13日期:2022-08-09 08:02:26

Java 8 最大的特性無異于更多地面向函數,比如引入了lambda等,可以更好地進行函數式編程。前段時間無意間發現了map.merge()方法,感覺還是很好用的,此文簡單做一些相關介紹。首先我們先看一個例子。

merge()怎么用?

假設我們有這么一段業務邏輯,我有一個學生成績對象的列表,對象包含學生姓名、科目、科目分數三個屬性,要求求得每個學生的總成績。加入列表如下:

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; }

我們先看一下常規做法:

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 值對應的 value,其源碼如下:

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; }

我們可以看到原理也是很簡單的,該方法接收三個參數,一個 key 值,一個 value,一個remappingFunction,如果給定的key不存在,它就變成了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()同理。所以,源碼肯定是要看的,不懂的地方多讀多練自然就理解了。

鏈接

參考:

www.jianshu.com/p/68e6b3041…

測試代碼地址:

github.com/lq920320/al…

到此這篇關于Java 8 中 Map 騷操作之 merge() 的用法 的文章就介紹到這了,更多相關Java 8 merge()用法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美一区二区在线观看视频 | 亚洲午夜影视 | 全免费a级毛片免费看视频免 | 国产真实自拍 | 黄频免费影院 | 97操碰 | 中文字幕一区二区视频 | 久久久久9999 | 免费的三级网站 | 538prom精品视频在放免费 | 亚洲欧美日韩在线不卡中文 | 亚洲精品久久久午夜伊人 | a毛片免费全部播放毛 | 一级网站在线观看 | 精品在线视频免费观看 | 欧美成人亚洲综合精品欧美激情 | 国产一级毛片免 | 免费观看日本高清a毛片 | 日本免费高清一区 | 国产在线欧美日韩精品一区二区 | 久久久久国产精品免费免费 | 亚洲黄色小视频 | 免费一级欧美大片在线观看 | 国产性自爱拍偷在在线播放 | 手机毛片免费看 | 国产三级a三级三级天天 | 国产欧美日韩精品一区二 | 精品国产一区二区二三区在线观看 | 国产手机在线视频放线视频 | 在线观看欧美亚洲日本专区 | 亚洲成a人v大片在线观看 | 国产欧美日韩在线视频 | 成人国产在线不卡视频 | 欧美一区二区亚洲 | 一 级 黄 色 大片 | 在线播放 亚洲 | 欧美成人www在线观看网页 | av大片| 视频一区在线免费观看 | a毛片全部播放免费视频完整18 | 亚洲国产成a人v在线 |