@Test
public void test() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
map.put("key", "22");
System.out.println(map.putIfAbsent("key", "33")); //打印:22
System.out.println(map.putIfAbsent("test", "44")); //打印:null
}
解释: putIfAbsent方法,添加的时候,看key是否存在,如果已存在,则返回map中该key对应的value
如果不存在,则添加成功,并返回null |