map computeIfAbsent 方法使用
所属分类 java
浏览量 1402
public V computeIfAbsent(K key, Function super K, ? extends V> mappingFunction)
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
public class ComputIfAbsentTest {
private static Map map = new ConcurrentHashMap();
private static void add(String key,long value){
AtomicLong al = map.get(key);
if(al==null){
al = new AtomicLong();
map.put(key, al);
}
al.addAndGet(value);
}
private static void addUseComputeIfAbsent(String key,long value){
map.computeIfAbsent(key, k-> new AtomicLong()).addAndGet(value);
}
public static void main(String[] args) throws Exception {
System.out.println("ComputIfAbsentTest start "+new Date());
add("a",2);
addUseComputeIfAbsent("b",3);
System.out.println(map);
}
}
上一篇
下一篇
Java实用工具类
mysql索引失效的几种情况
LongAdder vs AtomicLong
微服务架构优雅停机
基本的社交礼仪
Cookie Session 和 Token