ConcurrentHashMap類的computeIfAbsent(Key,Function)方法,如果鍵尚未與某個值關聯(或映射為null),則嘗試使用給定鍵的給定映射函數計算其值,然後在映射中輸入該計算出的值,否則為null 。
- 如果此方法的映射函數返回null,則不會在map中記錄新鍵的值。
- 此方法用於自動更新ConcurrentHashMap中給定鍵的值。
- 如果重新映射函數引發異常,則重新引發該異常,並且不記錄任何映射。
- 在計算過程中,不允許使用此方法修改此映射,因為其他線程也可以使用此映射。
用法:
public V computeIfAbsent(K key, Function<? super K, ? extends V> remappingFunction)
參數:此方法接受兩個參數:
- key與值關聯的:key。
- remappingFunction:function對值進行運算。
返回值:此方法返回與指定鍵關聯的當前(現有的或計算的)值;如果映射返回null,則返回null。
異常:該方法拋出:
- NullPointerException :如果指定的鍵或remappingFunction為null。
- llegalStateException:如果計算嘗試對此Map進行遞歸更新,否則將永遠無法完成。
- RuntimeException:如果remappingFunction這樣做,則映射不變。
以下示例程序旨在說明computeIfAbsent(Key,Function)方法:
程序1:
// Java program to demonstrate
// computeIfAbsent(Key, Function) 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("Pencil", 1000);
map.put("Laptop", 55000);
map.put("Clothes", 4400);
map.put("Mobile", 5300);
// print map details
System.out.println("ConcurrentHashMap:\n "
+ map.toString());
// provide value for new key which is absent
// using computeIfAbsent method
map.computeIfAbsent("PC", k -> 60000);
map.computeIfAbsent("Charger", k -> 800);
// print new mapping
System.out.println("new ConcurrentHashMap:\n "
+ map);
}
}
輸出:
ConcurrentHashMap: {Laptop=55000, Clothes=4400, Pencil=1000, Mobile=5300} new ConcurrentHashMap: {Laptop=55000, PC=60000, Clothes=4400, Pencil=1000, Charger=800, Mobile=5300}
程序2:
// Java program to demonstrate
// computeIfAbsent(Key, Function) 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, "1000RS");
map.put(2, "5009RS");
map.put(3, "1300RS");
// print map details
System.out.println("ConcurrentHashMap:\n"
+ map.toString());
// provide value for new key which is absent
// using computeIfAbsent method
map.computeIfAbsent(4, k -> "6000RS");
// this will not effect anything
// because key 1 is present
map.computeIfAbsent(1, k -> "8000RS");
// print new mapping
System.out.println("new ConcurrentHashMap:\n"
+ map);
}
}
輸出:
ConcurrentHashMap: {1=1000RS, 2=5009RS, 3=1300RS} new ConcurrentHashMap: {1=1000RS, 2=5009RS, 3=1300RS, 4=6000RS}
程序3:顯示NullPointerException
// Java program to demonstrate NullPointerException of
// computeIfAbsent(Key, Function) 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("Pencil", 1000);
map.put("Laptop", 55000);
map.put("Clothes", 4400);
map.put("Mobile", 5300);
try {
// provide value for new key which is absent
// using computeIfAbsent method
map.computeIfAbsent(null, k -> 60000);
}
catch (Exception e) {
// print new mapping
System.out.println("Exception:"
+ e);
}
}
}
輸出:
Exception:java.lang.NullPointerException
參考:https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent-K-java.util.function.Function-
相關用法
- Java Hashtable computeIfAbsent()用法及代碼示例
- Java HashMap computeIfAbsent()用法及代碼示例
- Java Properties computeIfAbsent(Key, Function)用法及代碼示例
- Java ConcurrentHashMap contains()用法及代碼示例
- Java ConcurrentHashMap entrySet()用法及代碼示例
- Java ConcurrentHashMap compute()用法及代碼示例
- Java ConcurrentHashMap elements()用法及代碼示例
- Java ConcurrentHashMap keys()用法及代碼示例
- Java ConcurrentHashMap keySet()用法及代碼示例
- Java ConcurrentHashMap values()用法及代碼示例
- Java ConcurrentHashMap get()用法及代碼示例
- Java ConcurrentHashMap put()用法及代碼示例
- Java ConcurrentHashMap putAll()用法及代碼示例
- Java ConcurrentHashMap putIfAbsent()用法及代碼示例
- Java ConcurrentHashMap isEmpty()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ConcurrentHashMap computeIfAbsent() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。