日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java8 stream 分组_Java 8 中 Map 骚操作之 merge() 的用法

發布時間:2025/3/15 java 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java8 stream 分组_Java 8 中 Map 骚操作之 merge() 的用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者:LQ木頭來源:http://juejin.im/post/5d9b455ae51d45782b0c1bfb

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

merge() 怎么用?

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

private List buildATestList() { List 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 studentScoreList = buildATestList(); Map 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 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) 的源碼,其返回值是計算后得到的新值:

總結

本文簡單介紹了一下 Map.merge() 的方法,除此之外,Java 8 中的 HashMap 實現方法使用了 TreeNode 和 紅黑樹,在源碼閱讀上可能有一點難度,不過原理上還是相似的,compute() 同理。所以,源碼肯定是要看的,不懂的地方多讀多練自然就理解了。

鏈接

參考:

https://www.jianshu.com/p/68e6b30410b0

測試代碼地址:

https://github.com/lq920320/algorithm-java-test/blob/master/src/test/java/other/MapMethodsTest.java

總結

以上是生活随笔為你收集整理的java8 stream 分组_Java 8 中 Map 骚操作之 merge() 的用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。