Java中的 ConcurrentHashMap computeIfAbsent() 方法及示例
ConcurrentHashMap 类的computeIfAbsent(Key, 函数)方法,如果 key尚未与某个值关联(或映射到 null),则尝试使用给定的映射函数计算其值,并在映射中输入该计算值 else null .
- 如果该方法的映射函数返回null,则map中的新键不记录值。
- 此方法用于自动更新 ConcurrentHashMap 中给定键的值。
- 如果重映射函数抛出异常,则重新抛出异常,并记录无映射。
- 在计算过程中,不允许使用此方法修改此映射,因为其他线程也可以使用此映射。
句法:
public V
computeIfAbsent(K key,
Function super K, ? extends V> remappingFunction)
参数:此方法接受两个参数:
- key :与值关联的键。
- remappingFunction : 对值进行操作的函数。
返回:此方法返回与指定键关联的当前(现有或计算)值,如果映射返回 null ,则返回 null 。
异常:此方法抛出:
- NullPointerException:如果指定的键或 remappingFunction 为空。
- llegalStateException:如果计算尝试对此地图进行递归更新,否则将永远不会完成。
- RuntimeException:如果 remappingFunction 这样做,在这种情况下映射不会改变。
下面的程序说明了 computeIfAbsent(Key, 函数) 方法:
方案一:
Java
// 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 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);
}
}
Java
// 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 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);
}
}
Java
// 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 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);
}
}
}
输出:
ConcurrentHashMap :
{Laptop=55000, Clothes=4400, Pencil=1000, Mobile=5300}
new ConcurrentHashMap :
{Laptop=55000, PC=60000, Clothes=4400, Pencil=1000, Charger=800, Mobile=5300}
方案二:
Java
// 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 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
// 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 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: Java/util/concurrent/ConcurrentHashMap.html#computeIfAbsent-K-java.util。函数。函数-