最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

源碼解析JDK 1.8 中的 Map.merge()

 更新時間:2019年10月10日 10:06:28   作者:風塵博客  
這篇文章主要介紹了JDK 1.8 之 Map.merge()的相關知識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下

Map 中ConcurrentHashMap是線程安全的,但不是所有操作都是,例如get()之后再put()就不是了,這時使用merge()確保沒有更新會丟失。

因為Map.merge()意味著我們可以原子地執(zhí)行插入或更新操作,它是線程安全的。

一、源碼解析

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

該方法接收三個參數,一個 key 值,一個 value,一個 remappingFunction 。如果給定的key不存在,它就變成了put(key, value);但是,如果key已經存在一些值,我們 remappingFunction 可以選擇合并的方式:

  • 只返回新值即可覆蓋舊值: (old, new) -> new;
  • 只需返回舊值即可保留舊值:(old, new) -> old;
  • 合并兩者,例如:(old, new) -> old + new;
  • 刪除舊值:(old, new) -> null。

二、使用場景

merge()方法在統(tǒng)計時用的場景比較多,例如:有一個學生成績對象的列表,對象包含學生姓名、科目、科目分數三個屬性,求得每個學生的總成績。

2.1 準備數據

學生對象StudentEntity.java

@Data
public class StudentEntity {
 /**
  * 學生姓名
  */
 private String studentName;
 /**
  * 學科
  */
 private String subject;
 /**
  * 分數
  */
 private Integer score;
}

學生成績數據

private List<StudentEntity> buildATestList() {
 List<StudentEntity> studentEntityList = new ArrayList<>();
 StudentEntity studentEntity1 = new StudentEntity() {{
  setStudentName("張三");
  setSubject("語文");
  setScore(60);
 }};
 StudentEntity studentEntity2 = new StudentEntity() {{
  setStudentName("張三");
  setSubject("數學");
  setScore(70);
 }};
 StudentEntity studentEntity3 = new StudentEntity() {{
  setStudentName("張三");
  setSubject("英語");
  setScore(80);
 }};
 StudentEntity studentEntity4 = new StudentEntity() {{
  setStudentName("李四");
  setSubject("語文");
  setScore(85);
 }};
 StudentEntity studentEntity5 = new StudentEntity() {{
  setStudentName("李四");
  setSubject("數學");
  setScore(75);
 }};
 StudentEntity studentEntity6 = new StudentEntity() {{
  setStudentName("李四");
  setSubject("英語");
  setScore(65);
 }};
 StudentEntity studentEntity7 = new StudentEntity() {{
  setStudentName("王五");
  setSubject("語文");
  setScore(80);
 }};
 StudentEntity studentEntity8 = new StudentEntity() {{
  setStudentName("王五");
  setSubject("數學");
  setScore(85);
 }};
 StudentEntity studentEntity9 = new StudentEntity() {{
  setStudentName("王五");
  setSubject("英語");
  setScore(90);
 }};

 studentEntityList.add(studentEntity1);
 studentEntityList.add(studentEntity2);
 studentEntityList.add(studentEntity3);
 studentEntityList.add(studentEntity4);
 studentEntityList.add(studentEntity5);
 studentEntityList.add(studentEntity6);
 studentEntityList.add(studentEntity7);
 studentEntityList.add(studentEntity8);
 studentEntityList.add(studentEntity9);

 return studentEntityList;
}

2.2 一般方案

思路:用Map的一組key/value存儲一個學生的總成績(學生姓名作為key,總成績?yōu)関alue)

Map中不存在指定的key時,將傳入的value設置為key的值;

當key存在值時,取出存在的值與當前值相加,然后放入Map中。

public void normalMethod() {
 Long startTime = System.currentTimeMillis();
 // 造一個學生成績列表
 List<StudentEntity> studentEntityList = buildATestList();

 Map<String, Integer> studentScore = new HashMap<>();
 studentEntityList.forEach(studentEntity -> {
  if (studentScore.containsKey(studentEntity.getStudentName())) {
   studentScore.put(studentEntity.getStudentName(),
     studentScore.get(studentEntity.getStudentName()) + studentEntity.getScore());
  } else {
   studentScore.put(studentEntity.getStudentName(), studentEntity.getScore());
  }
 });
 log.info("各個學生成績:{},耗時:{}ms",studentScore, System.currentTimeMillis() - startTime);
}

2.3 Map.merge()

很明顯,這里需要采用remappingFunction的合并方式。

public void mergeMethod() {
 Long startTime = System.currentTimeMillis();
 // 造一個學生成績列表
 List<StudentEntity> studentEntityList = buildATestList();
 Map<String, Integer> studentScore = new HashMap<>();
 studentEntityList.forEach(studentEntity -> studentScore.merge(
   studentEntity.getStudentName(),
   studentEntity.getScore(),
   Integer::sum));
 log.info("各個學生成績:{},耗時:{}ms",studentScore, System.currentTimeMillis() - startTime);
}

2.4 測試及小結

測試方法

@Test
public void testAll() {
 // 一般寫法
 normalMethod();
 // merge()方法
 mergeMethod();
}

測試結果

00:21:28.305 [main] INFO cn.van.jdk.eight.map.merge.MapOfMergeTest - 各個學生成績:{李四=225, 張三=210, 王五=255},耗時:75ms
00:21:28.310 [main] INFO cn.van.jdk.eight.map.merge.MapOfMergeTest - 各個學生成績:{李四=225, 張三=210, 王五=255},耗時:2ms

結果小結

  • merger()方法使用起來在一定程度上減少了代碼量,使得代碼更加簡潔。同時,通過打印的方法耗時可以看出,merge()方法效率更高。
  • Map.merge()的出現,和ConcurrentHashMap的結合,完美處理那些自動執(zhí)行插入或者更新操作的單線程安全的邏輯.

三、總結

3.1 示例源碼

Github 示例代碼

總結

以上所述是小編給大家介紹的JDK 1.8 中的 Map.merge(),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

相關文章

  • Java數據結構常見幾大排序梳理

    Java數據結構常見幾大排序梳理

    Java常見的排序算法有:直接插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序等。本文詳解介紹它們的實現以及圖解,需要的可以參考一下
    2022-03-03
  • Java 多線程死鎖的產生以及如何避免死鎖

    Java 多線程死鎖的產生以及如何避免死鎖

    這篇文章主要介紹了Java 多線程死鎖的產生以及如何避免死鎖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Java如何實現讀取txt文件內容并生成Word文檔

    Java如何實現讀取txt文件內容并生成Word文檔

    本文主要介紹了通過Java實現讀取txt文件中的內容,并將內容生成Word文檔。文章的代碼非常詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下
    2021-12-12
  • Java集合的定義與Collection類使用詳解

    Java集合的定義與Collection類使用詳解

    這篇文章主要介紹了Java集合的定義及Collection工具類使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-11-11
  • Java隨機數的5種獲得方法(非常詳細!)

    Java隨機數的5種獲得方法(非常詳細!)

    這篇文章主要給大家介紹了關于Java隨機數的5種獲得方法,在實際開發(fā)中產生隨機數的使用是很普遍的,所以在程序中進行產生隨機數操作很重要,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-10-10
  • Spring存儲與讀取Bean對象方法

    Spring存儲與讀取Bean對象方法

    在Spring中,要想更簡單的存儲和讀取對象的核心是使用注解,這篇文章主要給大家介紹了關于Spring如何通過注解存儲和讀取對象的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • java中調用super的實例講解

    java中調用super的實例講解

    在本篇文章里小編給大家分享了一篇關于java中調用super的實例講解內容,有興趣的朋友們可以學習下。
    2020-12-12
  • 接口簽名怎么用Java實現

    接口簽名怎么用Java實現

    今天帶大家學習java的相關知識,文章圍繞怎么用Java實現接口簽名展開,文中有非常詳細的代碼示例及介紹,需要的朋友可以參考下
    2021-06-06
  • 淺談Java中向上造型向下造型和接口回調中的問題

    淺談Java中向上造型向下造型和接口回調中的問題

    這篇文章主要介紹了淺談Java中向上造型向下造型和接口回調中的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java中的MapStruct用法詳解

    Java中的MapStruct用法詳解

    這篇文章主要介紹了Java中的MapStruct用法詳解,MapStuct的使用非常簡單,把對應的jar包引入即可,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-04-04

最新評論

波密县| 普定县| 迁西县| 五指山市| 云霄县| 虞城县| 周至县| 务川| 洛浦县| 资阳市| 会同县| 余江县| 汾西县| 大荔县| 于都县| 赫章县| 鄄城县| 兴海县| 永嘉县| 永仁县| 阿巴嘎旗| 运城市| 蒙自县| 榆社县| 库伦旗| 鹤山市| 英吉沙县| 潜山县| 余干县| 中江县| 晋宁县| 乌鲁木齐市| 乌拉特前旗| 三门峡市| 江口县| 太白县| 凤庆县| 都匀市| 五指山市| 兴海县| 陕西省|