浙江沉船事故最新消息,南昌seo报价,安徽网站建设合肥网站建设,北京活动策划公司黄页Java putIfAbsent() 方法详解
在 Java 中#xff0c;putIfAbsent() 是 Map 接口中的一个方法#xff0c;从 Java 8 开始引入。它用于向映射中添加一个键值对#xff0c;只有在该键尚未存在时才进行添加操作。如果键已存在#xff0c;则不会覆盖原有值。 1. 方法定义
方法…Java putIfAbsent() 方法详解
在 Java 中putIfAbsent() 是 Map 接口中的一个方法从 Java 8 开始引入。它用于向映射中添加一个键值对只有在该键尚未存在时才进行添加操作。如果键已存在则不会覆盖原有值。 1. 方法定义
方法签名
default V putIfAbsent(K key, V value)参数
key要插入的键。value与键关联的值。
返回值
如果键不存在插入后返回 null。如果键已存在则返回该键当前的值插入操作不会执行。 2. 功能描述 检查键是否存在 如果键不存在则将键值对插入到映射中。如果键已存在则保持原有键值对不变。 线程安全 对于并发映射如 ConcurrentHashMapputIfAbsent() 是线程安全的保证了原子性。对于普通 HashMap则不是线程安全的。 避免覆盖现有值 与直接调用 put() 不同putIfAbsent() 不会覆盖现有的值。 3. 示例代码
3.1 基本用法
import java.util.HashMap;
import java.util.Map;public class PutIfAbsentExample {public static void main(String[] args) {MapString, String map new HashMap();// 初始插入map.put(A, Apple);// 插入新键map.putIfAbsent(B, Banana);System.out.println(map); // 输出{AApple, BBanana}// 尝试插入已存在的键map.putIfAbsent(A, Avocado);System.out.println(map); // 输出{AApple, BBanana}}
}分析
初次插入键 A 和 B。对于键 AputIfAbsent() 不会覆盖原值因此保持不变。 3.2 结合返回值
import java.util.HashMap;
import java.util.Map;public class PutIfAbsentReturnExample {public static void main(String[] args) {MapString, String map new HashMap();// 尝试插入新键String result1 map.putIfAbsent(C, Cat);System.out.println(result1); // 输出null键 C 不存在// 再次尝试插入相同键String result2 map.putIfAbsent(C, Carrot);System.out.println(result2); // 输出Cat键 C 已存在值保持为 CatSystem.out.println(map); // 输出{CCat}}
}3.3 使用 ConcurrentHashMap
putIfAbsent() 在 ConcurrentHashMap 中非常有用可以实现线程安全的惰性初始化。
import java.util.concurrent.ConcurrentHashMap;public class ConcurrentPutIfAbsent {public static void main(String[] args) {ConcurrentHashMapString, Integer map new ConcurrentHashMap();// 多线程同时尝试插入map.putIfAbsent(key, 1);map.putIfAbsent(key, 2);System.out.println(map); // 输出{key1}只插入一次}
}4. putIfAbsent() 和 put() 的区别
特性put()putIfAbsent()覆盖值如果键已存在则覆盖旧值。如果键已存在则不覆盖旧值。返回值返回旧值如果存在否则返回 null。如果键已存在返回旧值否则返回 null。性能直接插入操作可能覆盖原值。需要额外检查键是否存在线程安全时也加锁。线程安全ConcurrentMap不是线程安全的需要额外同步。线程安全尤其适用于 ConcurrentHashMap。 5. 使用场景
5.1 避免覆盖已存在值
当希望保持某个键的初始值避免被后续操作覆盖时
map.putIfAbsent(key, initialValue);5.2 延迟初始化
在多线程环境中putIfAbsent() 可以安全地初始化共享资源
public static ConcurrentHashMapString, String cache new ConcurrentHashMap();public static String getValue(String key) {return cache.putIfAbsent(key, DefaultValue);
}5.3 统计或计数
可以用 putIfAbsent() 初始化键的默认值用于统计场景
map.putIfAbsent(count, 0);
map.put(count, map.get(count) 1);6. 注意事项 线程安全 对普通的 HashMap 使用 putIfAbsent() 并不能实现线程安全。如果需要线程安全请使用 ConcurrentHashMap 或其他并发集合。 返回值的使用 返回值可以用来判断键是否已存在从而决定后续操作。 性能开销 对于并发集合如 ConcurrentHashMapputIfAbsent() 内部使用了锁来保证原子性可能有一定性能开销。 不可用于 null 值 putIfAbsent() 不允许插入 null 值ConcurrentHashMap 会抛出 NullPointerException。 7. 总结
putIfAbsent() 是一种安全的插入操作 如果键不存在则插入键值对。如果键已存在则保持原值不变。 线程安全性 在 ConcurrentHashMap 中putIfAbsent() 是线程安全的可用于多线程环境。 适用场景 避免值覆盖。延迟初始化或缓存加载。实现统计或计数。
通过正确使用 putIfAbsent() 方法可以简化代码逻辑同时确保数据的完整性和安全性尤其在并发场景中非常实用。