您的位置:首页 > 其它

put与putIfAbsent区别

2016-11-10 00:00 148 查看
put与putIfAbsent区别,put在放入数据时,如果放入数据的key已经存在与Map中,最后放入的数据会覆盖之前存在的数据,而putIfAbsent在放入数据时,如果存在重复的key,那么putIfAbsent不会放入值。

底层实现:

public V put(K key, V value) {
if (value == null)
throw new NullPointerException();
int hash = hash(key.hashCode());
return segmentFor(hash).put(key, hash, value, false);
}
public V putIfAbsent(K key, V value) {
if (value == null)
throw new NullPointerException();
int hash = hash(key.hashCode());
return segmentFor(hash).put(key, hash, value, true);
}

put事例:

Map<Integer, String> map = new HashMap<>();
map.put(1, "张三");
map.put(2, "李四");
map.put(1, "王五");
map.forEach((key,value)->{
System.out.println("key = " + key + ", value = " + value);
});

输出结果:

key = 1, value = 王五
key = 2, value = 李四

putIfAbsent事例:

Map<Integer, String> putIfAbsent = new HashMap<>();
putIfAbsent.putIfAbsent(1, "张三");
putIfAbsent.putIfAbsent(2, "李四");
putIfAbsent.putIfAbsent(1, "王五");
putIfAbsent.forEach((key,value)->{
System.out.println("key = " + key + ", value = " + value);
});

输出结果:

key = 1, value = 张三
key = 2, value = 李四
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息