當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java ConcurrentHashMap compute()用法及代碼示例


ConcurrentHashMap類的compute(Key,BiFunction)方法用於計算指定鍵及其當前映射值的映射(如果找不到當前映射,則為null)。

  • 此方法用於自動更新ConcurrentHashMap中給定鍵的值。
  • 如果重映射函數引發異常,則重新引發該異常,並且當前映射保持不變。
  • 在計算過程中,該圖從其他線程的更新過程已被阻止,因此計算不得嘗試更新此圖的任何其他映射
  • 例如,此映射附加映射的字符串值:
    ConcurrentHashMap.compute(key, 
    (key, value) -> (value == null) ? msg:value.concat(msg))
    

用法:


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

參數:此方法接受兩個參數:

  • key與值關聯的:key。
  • remappingFunction:function對值進行運算。

返回值:此方法返回與指定鍵關聯的新值;如果沒有,則返回null。

異常:此方法引發folloeiong異常:

  • NullPointerException :如果指定的鍵或remappingFunction為null。
  • llegalStateException:如果計算嘗試對此Map進行遞歸更新,否則將永遠無法完成。
  • RuntimeException:如果remappingFunction這樣做,則映射不變。

以下示例程序旨在說明compute(Key,BiFunction)方法:

程序1:

// Java program to demonstrate 
// compute(Key, BiFunction) method. 
  
import java.util.concurrent.*; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
  
        // create a ConcurrentHashMap and add some values 
        ConcurrentHashMap<String, Integer> 
            map = new ConcurrentHashMap<>(); 
        map.put("Book1", 10); 
        map.put("Book2", 500); 
        map.put("Book3", 400); 
  
        // print map details 
        System.out.println("ConcurrentHashMap:"
                           + map.toString()); 
  
        // remap the values of ConcurrentHashMap 
        // using compute method 
        map.compute("Book2", (key, val) 
                                 -> val + 100); 
        map.compute("Book1", (key, val) 
                                 -> val + 512); 
  
        // print new mapping 
        System.out.println("New ConcurrentHashMap:"
                           + map); 
    } 
}
輸出:
ConcurrentHashMap:{Book3=400, Book1=10, Book2=500}
New ConcurrentHashMap:{Book3=400, Book1=522, Book2=600}

程序2:

// Java program to demonstrate 
// compute(Key, BiFunction) method. 
  
import java.util.concurrent.*; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
  
        // create a ConcurrentHashMap and add some values 
        ConcurrentHashMap<Integer, String> 
            map = new ConcurrentHashMap<>(); 
        map.put(1, "Kolkata"); 
        map.put(2, "Nadia"); 
        map.put(3, "Howrah"); 
  
        // print map details 
        System.out.println("ConcurrentHashMap:"
                           + map.toString()); 
  
        // remap the values of ConcurrentHashMap 
        // using compute method 
        map.compute(2, (key, val) 
                           -> val.concat(" (West-Bengal)")); 
        map.compute(3, (key, val) 
                           -> val.concat(" (West-Bengal)")); 
  
        // print new mapping 
        System.out.println("New ConcurrentHashMap:"
                           + map); 
    } 
}
輸出:
ConcurrentHashMap:{1=Kolkata, 2=Nadia, 3=Howrah}
New ConcurrentHashMap:{1=Kolkata, 2=Nadia (West-Bengal), 3=Howrah (West-Bengal)}

程序3:顯示NullPointerException

// Java program to demonstrate NullPointerException 
// for compute(Key, BiFunction) method. 
  
import java.util.concurrent.*; 
  
public class GFG { 
  
    // Main method 
    public static void main(String[] args) 
    { 
  
        // create a ConcurrentHashMap and add some values 
        ConcurrentHashMap<Integer, String> 
            map = new ConcurrentHashMap<>(); 
        map.put(1, "Kolkata"); 
        map.put(2, "Nadia"); 
        map.put(3, "Howrah"); 
  
        try { 
  
            // remap the values of ConcurrentHashMap 
            // using compute method 
            map.compute(null, (key, val) 
                                  -> val.concat(" (West-Bengal)")); 
        } 
        catch (NullPointerException e) { 
  
            // print Exception 
            System.out.println("Exception:" + e); 
        } 
    } 
}
輸出:
Exception:java.lang.NullPointerException

參考:https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#compute-K-java.util.function.BiFunction-



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ConcurrentHashMap compute() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。