<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 1. computeIfAbsent `computeIfAbsent(key, key -> {})` computeIfAbsent() 方法對 hashMap 中指定 key 的值進行重新計算,如果不存在這個 key,則添加到 hashMap 中。 ~~~ @Test public void testcomputeIfAbsent() { // 創建一個 HashMap HashMap<String, Integer> prices = new HashMap<>(); // 往HashMap中添加映射項 prices.put("Shoes", 200); prices.put("Bag", 300); prices.put("Pant", 150); System.out.println("HashMap: " + prices); // 計算 Shirt 的值,不存在則計算加入 int shirtPrice = prices.computeIfAbsent("Shirt", key -> 280); System.out.println("Price of Shirt: " + shirtPrice); // 輸出更新后的HashMap System.out.println("Updated HashMap: " + prices); } ~~~ 結果添加了不存在的key ``` HashMap: {Pant=150, Bag=300, Shoes=200} Price of Shirt: 280 Updated HashMap: {Pant=150, Shirt=280, Bag=300, Shoes=200} ``` # 2.compute `compute(key, (key, value) -> {})` 1. 值不存在則什么都不做 2. 有值就更新計算后的值,并返回 3. 如果值不存在,則函數照應執行,此時value=null,如果對值進行賦值,和 ~~~ /** * 對key對應的value進行計算,并重新賦值,然后返回新值 */ @Test public void test() { //創建一個 HashMap HashMap<String, Integer> prices = new HashMap<>(); // 往HashMap中添加映射項 prices.put("Shoes", 200); prices.put("Bag", 300); prices.put("Pant", 150); System.out.println("HashMap: " + prices); // 重新計算鞋子打了10%折扣后的值 int newPrice = prices.compute("Shoes", (key, value) -> value - value * 10 / 100); System.out.println("Discounted Price of Shoes: " + newPrice); //如果key不存在,則什么都不做 prices.compute("glass", (key, value) -> null); // 輸出更新后的HashMap System.out.println("Updated HashMap: " + prices); } ~~~ 輸出 ``` HashMap: {Pant=150, Bag=300, Shoes=200} Discounted Price of Shoes: 180 Updated HashMap: {Pant=150, Bag=300, Shoes=180} ``` 如果key不存在,但是通過函數給key賦值,和computeIfAbsent效果一樣,也添加一個計算后的key、value ~~~ @Test public void test1() { //創建一個 HashMap HashMap<String, Integer> prices = new HashMap<>(); // 往HashMap中添加映射項 prices.put("Shoes", 200); prices.put("Bag", 300); prices.put("Pant", 150); System.out.println("HashMap: " + prices); //如果key不存在,但是函數把value賦值了,和computeIfAbsent作用一樣 prices.compute("glass", (key, value) -> { int s = 0; if(Objects.isNull(value)) { s = 110; } return s; }); // 輸出更新后的HashMap System.out.println("Updated HashMap: " + prices); } ~~~ 增加了一個不存在的glass=110 ``` HashMap: {Pant=150, Bag=300, Shoes=200} Updated HashMap: {glass=110, Pant=150, Bag=300, Shoes=200} ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看