You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
18 lines
838 B
18 lines
838 B
一、存在问题
|
|
线程不安全
|
|
底层使用HashMap,多线程环境下并发操作会导致数据丢失、死循环等问题。
|
|
空值风险
|
|
允许key和value为null,业务中易引发NullPointerException,且无法区分 “键不存在” 和 “值为 null”。
|
|
无缓存淘汰机制
|
|
数据永久驻留内存,无过期 / 清理策略,长期运行会造成内存泄漏。
|
|
功能单一
|
|
缺少判断键是否存在、获取所有键 / 值等实用方法。
|
|
二、优化建议
|
|
保证线程安全
|
|
将HashMap替换为ConcurrentHashMap,无需加锁即可支持高并发。
|
|
禁止空值存入
|
|
在put方法中增加非空校验,拒绝null键 / 值。
|
|
完善工具方法
|
|
新增containsKey、isEmpty等方法,提升实用性。
|
|
增加基础防御
|
|
对get/remove传入null键时直接返回null,避免异常。
|