Concurrenthashmap的putIfAbsent方法

论坛 期权论坛 脚本     
匿名技术用户   2021-1-3 22:27   509   0

@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

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP