Java HashMap computeIfPresent() 方法



描述

Java HashMap computeIfPresent() 方法用於在指定鍵已與值關聯(或對映到 null)時,使用給定的對映函式計算該鍵的對映,並將其輸入到此對映中,除非為 null。

宣告

以下是 java.util.HashMap.computeIfPresent​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction) 方法的宣告。

public V computeIfPresent​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)

引數

key − 與指定值關聯的鍵

remappingFunction − 用於計算值的重新對映函式

返回值

方法呼叫返回與指定鍵關聯的新值,如果不存在則返回 null。

異常

ConcurrentModificationException − 如果檢測到重新對映函式修改了此對映。

整數、整數對 HashMap 的已對映鍵的計算對映示例

以下示例演示瞭如何使用 Java HashMap computeIfPresent() 方法獲取 Map 的更新值。我們建立了兩個整數、整數對的 Map 物件。然後向 map 中添加了一些條目,然後使用 computeIfPresent() 方法更新值,最後列印更新後的 map。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Integer> newmap = new HashMap<>();

      // populate map
      newmap.put(1, 1);
      newmap.put(3, 3); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfPresent(1, (key,value) -> value * 10);
      newmap.computeIfPresent(2, (key,value) -> value * 20);
      newmap.computeIfPresent(3, (key,value) -> value * 30);

      System.out.println("Updated Map: " + newmap);   
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Map: {1=1, 3=3}
Updated Map: {1=10, 3=90}

整數、字串對 HashMap 的未對映鍵的計算對映示例

以下示例演示瞭如何使用 Java HashMap compute() 方法獲取 Map 的更新值。我們建立了兩個整數、字串對的 Map 物件。然後向 map 中添加了一些條目,然後使用 computeIfPresent() 方法更新值,最後列印更新後的 map。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, String> newmap = new HashMap<>();

      // populate map
      newmap.put(1, "A");
      newmap.put(3, "C"); 
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfPresent(1, (key,value) -> value.concat("123"));
      newmap.computeIfPresent(2, (key,value) -> value.concat("456"));
      newmap.computeIfPresent(3, (key,value) -> value.concat("789"));

      System.out.println("Updated Map: " + newmap);   
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Map: {1=A, 2=B, 3=C}
Updated Map: {1=A123, 2=B456, 3=C789}

整數、學生對 HashMap 的未對映鍵的計算對映示例

以下示例演示瞭如何使用 Java HashMap compute() 方法獲取 Map 的更新值。我們建立了兩個整數、學生對的 Map 物件。然後向 map 中添加了一些條目,然後使用 computeIfPresent() 方法更新值,最後列印更新後的 map。

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Student> newmap = new HashMap<>();

      // populate map
      newmap.put(1, new Student(1, "Julie"));
      newmap.put(3, new Student(3, "Adam"));
     
      // print the map
      System.out.println("Map: " + newmap);
 
      // update the values of the map
      newmap.computeIfPresent(1, (key,value) -> value.update("Roberts"));
      newmap.computeIfPresent(2, (key,value) -> value.update("Pitts"));
      newmap.computeIfPresent(3, (key,value) -> value.update("Cruise"));

      System.out.println("Updated Map: " + newmap);   
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }
   
   public Student update(String surname) {
	   this.name = this.name.concat(" " + surname);
	   return this;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果。

Map: {1=[ 1, Julie ], 3=[ 3, Adam ]}
Updated Map: {1=[ 1, Julie Roberts ], 3=[ 3, Adam Cruise ]}
java_util_hashmap.htm
廣告

© . All rights reserved.