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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。