<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java 8 中的 Map 騷操作,學(xué)習(xí)下!

          共 2987字,需瀏覽 6分鐘

           ·

          2020-11-24 16:04

          • merge()?怎么用?
          • merge()?簡(jiǎn)介
          • 使用場(chǎng)景
          • 其他
          • 總結(jié)

          Java 8最大的特性無異于更多地面向函數(shù),有時(shí)約會(huì)了lambda等,可以更好地進(jìn)行函數(shù)式編程。

          前段時(shí)間無意間發(fā)現(xiàn)了map.merge()方法,感覺還是很好用的,此文簡(jiǎn)單做一些相關(guān)介紹。首先我們先看一個(gè)例子。

          merge()?怎么用?

          假設(shè)我們有這么一段業(yè)務(wù)邏輯,我有一個(gè)學(xué)生成績(jī)對(duì)象的列表,對(duì)象包含學(xué)生姓名,科目,科目分?jǐn)?shù)三個(gè)屬性,要求求得每個(gè)學(xué)生的總成績(jī)。

          加入列表如下:

          private?List?buildATestList()?{
          ????List?studentScoreList?=?new?ArrayList<>();
          ????StudentScore?studentScore1?=?new?StudentScore()?{{
          ????????setStuName("張三");
          ????????setSubject("語文");
          ????????setScore(70);
          ????}};
          ????StudentScore?studentScore2?=?new?StudentScore()?{{
          ????????setStuName("張三");
          ????????setSubject("數(shù)學(xué)");
          ????????setScore(80);
          ????}};
          ????StudentScore?studentScore3?=?new?StudentScore()?{{
          ????????setStuName("張三");
          ????????setSubject("英語");
          ????????setScore(65);
          ????}};
          ????StudentScore?studentScore4?=?new?StudentScore()?{{
          ????????setStuName("李四");
          ????????setSubject("語文");
          ????????setScore(68);
          ????}};
          ????StudentScore?studentScore5?=?new?StudentScore()?{{
          ????????setStuName("李四");
          ????????setSubject("數(shù)學(xué)");
          ????????setScore(70);
          ????}};
          ????StudentScore?studentScore6?=?new?StudentScore()?{{
          ????????setStuName("李四");
          ????????setSubject("英語");
          ????????setScore(90);
          ????}};
          ????StudentScore?studentScore7?=?new?StudentScore()?{{
          ????????setStuName("王五");
          ????????setSubject("語文");
          ????????setScore(80);
          ????}};
          ????StudentScore?studentScore8?=?new?StudentScore()?{{
          ????????setStuName("王五");
          ????????setSubject("數(shù)學(xué)");
          ????????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;
          }

          我們先看一下常規(guī)做法:

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

          //?結(jié)果如下:
          //?{"李四":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));

          //?結(jié)果如下:
          //?{"李四":228,"張三":215,"王五":235}

          merge()?簡(jiǎn)介

          merge()?可以這么理解:不斷新的值賦值到key(如果不存在)或更新給定的key值對(duì)應(yīng)的值,其源碼如下:

          default?V?merge(K?key,?V?value,?BiFunction?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;
          }

          我們可以看到原理也是很簡(jiǎn)單的,該方法接收三個(gè)參數(shù),一個(gè)鍵值,一個(gè)值,一個(gè)remappingFunction,如果給定的鍵不存在,它就變成了put(key, value)

          但是,如果key已經(jīng)存在一些值,我們remappingFunction可以選擇合并的方式,然后將合并得到的newValue賦值給原先的key。

          使用場(chǎng)景

          這個(gè)使用場(chǎng)景相對(duì)來說還是比較多的,某種分組求和這類的操作,雖然stream中有相關(guān)groupingBy()方法,但是如果你想在循環(huán)中做一些其他操作的時(shí)候,merge()還是一個(gè)挺不錯(cuò)的選擇的。

          其他

          除了merge()方法之外,我還看到了一些的Java 8中map相關(guān)的其他方法,比如putIfAbsent, ,compute()computeIfAbsent()computeIfPresent這些方法我們看名字應(yīng)該就知道是什么意思了

          故此就不做過多介紹了,研究的可以簡(jiǎn)單閱讀一下原始碼(都還是挺易懂的)。

          這里我們貼一下compute()(Map.class)的源碼,其返回值是計(jì)算后得到的新值:

          default?V?compute(K?key,?BiFunction?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;
          ????}
          }

          總結(jié)

          本文簡(jiǎn)單介紹了一下Map.merge()的方法,另外,Java 8中的HashMap實(shí)現(xiàn)方法使用了TreeNode和紅黑樹,在源碼閱讀上可能有一點(diǎn)缺點(diǎn),不過原理上還是相似的,compute()同理。

          所以,原始碼肯定是要看的,不懂的地方多讀多練自然就理解了。

          源:juejin.im/post/6844903958280945677

          版權(quán)申明:內(nèi)容來源網(wǎng)絡(luò),版權(quán)歸原創(chuàng)者所有。除非無法確認(rèn),我們都會(huì)標(biāo)明作者及出處,如有侵權(quán)煩請(qǐng)告知,我們會(huì)立即刪除并表示歉意。謝謝!





          感謝閱讀



          瀏覽 39
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久国产视觉盛宴 | 蜜乳AV一区二区 | 四虎8848精品成人免费网站 | 婷婷五月丁香网 | 午夜成人精品偷拍在线 |