欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > Java putIfAbsent() 详解

Java putIfAbsent() 详解

2025/5/21 7:26:12 来源:https://blog.csdn.net/T_Y_F_/article/details/143867823  浏览:    关键词:Java putIfAbsent() 详解

Java putIfAbsent() 方法详解

在 Java 中,putIfAbsent()Map 接口中的一个方法,从 Java 8 开始引入。它用于向映射中添加一个键值对,只有在该键尚未存在时才进行添加操作。如果键已存在,则不会覆盖原有值。


1. 方法定义

方法签名
default V putIfAbsent(K key, V value)
参数
  • key:要插入的键。
  • value:与键关联的值。
返回值
  • 如果键不存在,插入后返回 null
  • 如果键已存在,则返回该键当前的值,插入操作不会执行。

2. 功能描述

  • 检查键是否存在

    • 如果键不存在,则将键值对插入到映射中。
    • 如果键已存在,则保持原有键值对不变。
  • 线程安全

    • 对于并发映射(如 ConcurrentHashMap),putIfAbsent() 是线程安全的,保证了原子性。
    • 对于普通 HashMap,则不是线程安全的。
  • 避免覆盖现有值

    • 与直接调用 put() 不同,putIfAbsent() 不会覆盖现有的值。

3. 示例代码

3.1 基本用法
import java.util.HashMap;
import java.util.Map;public class PutIfAbsentExample {public static void main(String[] args) {Map<String, String> map = new HashMap<>();// 初始插入map.put("A", "Apple");// 插入新键map.putIfAbsent("B", "Banana");System.out.println(map); // 输出:{A=Apple, B=Banana}// 尝试插入已存在的键map.putIfAbsent("A", "Avocado");System.out.println(map); // 输出:{A=Apple, B=Banana}}
}

分析

  1. 初次插入键 AB
  2. 对于键 AputIfAbsent() 不会覆盖原值,因此保持不变。

3.2 结合返回值
import java.util.HashMap;
import java.util.Map;public class PutIfAbsentReturnExample {public static void main(String[] args) {Map<String, 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" 已存在,值保持为 "Cat")System.out.println(map); // 输出:{C=Cat}}
}

3.3 使用 ConcurrentHashMap

putIfAbsent()ConcurrentHashMap 中非常有用,可以实现线程安全的惰性初始化。

import java.util.concurrent.ConcurrentHashMap;public class ConcurrentPutIfAbsent {public static void main(String[] args) {ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();// 多线程同时尝试插入map.putIfAbsent("key", 1);map.putIfAbsent("key", 2);System.out.println(map); // 输出:{key=1}(只插入一次)}
}

4. putIfAbsent()put() 的区别

特性put()putIfAbsent()
覆盖值如果键已存在,则覆盖旧值。如果键已存在,则不覆盖旧值。
返回值返回旧值(如果存在),否则返回 null如果键已存在,返回旧值,否则返回 null
性能直接插入操作,可能覆盖原值。需要额外检查键是否存在(线程安全时也加锁)。
线程安全(ConcurrentMap)不是线程安全的,需要额外同步。线程安全,尤其适用于 ConcurrentHashMap

5. 使用场景

5.1 避免覆盖已存在值

当希望保持某个键的初始值,避免被后续操作覆盖时:

map.putIfAbsent("key", "initialValue");
5.2 延迟初始化

在多线程环境中,putIfAbsent() 可以安全地初始化共享资源:

public static ConcurrentHashMap<String, 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. 注意事项

  1. 线程安全

    • 对普通的 HashMap 使用 putIfAbsent() 并不能实现线程安全。
    • 如果需要线程安全,请使用 ConcurrentHashMap 或其他并发集合。
  2. 返回值的使用

    • 返回值可以用来判断键是否已存在,从而决定后续操作。
  3. 性能开销

    • 对于并发集合(如 ConcurrentHashMap),putIfAbsent() 内部使用了锁来保证原子性,可能有一定性能开销。
  4. 不可用于 null

    • putIfAbsent() 不允许插入 null 值,ConcurrentHashMap 会抛出 NullPointerException

7. 总结

  • putIfAbsent() 是一种安全的插入操作
    • 如果键不存在,则插入键值对。
    • 如果键已存在,则保持原值不变。
  • 线程安全性
    • ConcurrentHashMap 中,putIfAbsent() 是线程安全的,可用于多线程环境。
  • 适用场景
    • 避免值覆盖。
    • 延迟初始化或缓存加载。
    • 实现统计或计数。

通过正确使用 putIfAbsent() 方法,可以简化代码逻辑,同时确保数据的完整性和安全性,尤其在并发场景中非常实用。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词