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-
相关用法
- Java ConcurrentHashMap contains()用法及代码示例
- Java ConcurrentHashMap entrySet()用法及代码示例
- Java ConcurrentHashMap computeIfAbsent()用法及代码示例
- Java ConcurrentHashMap keys()用法及代码示例
- Java ConcurrentHashMap values()用法及代码示例
- Java ConcurrentHashMap keySet()用法及代码示例
- Java ConcurrentHashMap elements()用法及代码示例
- Java HashMap compute()用法及代码示例
- Java HashTable compute()用法及代码示例
- Java Properties compute(Key, BiFunction)用法及代码示例
- Java ConcurrentHashMap put()用法及代码示例
- Java ConcurrentHashMap get()用法及代码示例
- Java ConcurrentHashMap putIfAbsent()用法及代码示例
- Java ConcurrentHashMap remove()用法及代码示例
- Java ConcurrentHashMap putAll()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ConcurrentHashMap compute() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。