当前位置: 首页 > news >正文

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() 方法,可以简化代码逻辑,同时确保数据的完整性和安全性,尤其在并发场景中非常实用。

http://www.lryc.cn/news/487073.html

相关文章:

  • 使用PSpice进行第一个电路的仿真
  • 路漫漫其修远兮,吾将上下而求索---第一次使用github的过程记录和个人感受
  • 【微软:多模态基础模型】(4)统一视觉模型
  • GRS码(Generalized Reed-Solomon Code)
  • 三、谷粒商城- Spring Cloud Alibaba(3)
  • MATLAB和Python激发光谱
  • 学习笔记024——Ubuntu 安装 Redis遇到相关问题
  • UE5 腿部IK 解决方案 footplacement
  • 北航软件算法C4--图部分
  • PCL点云开发-解决在Qt中嵌入点云窗口出现的一闪而过的黑窗口
  • 本地音乐服务器(二)
  • 第三十六篇——伯努利试验:到底如何理解随机性?
  • 【Android、IOS、Flutter、鸿蒙、ReactNative 】屏幕适配
  • candence : 如何利用EXCEL 绘制复杂、多管脚元件
  • 项目配置文件选择(Json,xml,Yaml, INI)
  • Android 使用Retrofit 以纯二进制文件流上传文件
  • Vue3踩坑记录
  • 大数据-227 离线数仓 - Flume 自定义拦截器(续接上节) 采集启动日志和事件日志
  • 【热门主题】000054 ECMAScript:现代 Web 开发的核心语言
  • 【Pytorch】torch.nn.functional模块中的非线性激活函数
  • reactflow 中 useNodesState 模块作用
  • Go语言内存分配源码分析学习笔记
  • 【jvm】方法区常用参数有哪些
  • JAVA环境的配置
  • LLM文档对话 —— pdf解析关键问题
  • MySQL单表查询时索引使用情况
  • Qt邮箱程序改良版(信号和槽)
  • 入门到精通mysql数据(四)
  • Java 设计模式 详解
  • 卡尔曼滤波学习资料汇总