修复特定情况下BiMap覆盖Value后,仍能通过旧Value查询到Key问题

This commit is contained in:
Looly 2023-10-18 08:57:30 +08:00
parent 5d8a411453
commit e46474fc9a
3 changed files with 25 additions and 1 deletions

View File

@ -2,7 +2,7 @@
# 🚀Changelog # 🚀Changelog
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.23(2023-10-08) # 5.8.23(2023-10-18)
### 🐣新特性 ### 🐣新特性
* 【json 】 改进TemporalAccessorSerializer支持dayOfMonth和month枚举名issue#I82AM8@Gitee * 【json 】 改进TemporalAccessorSerializer支持dayOfMonth和month枚举名issue#I82AM8@Gitee
@ -14,6 +14,7 @@
* 【cron 】 修复Cron表达式range解析错误问题issue#I82CSH@Gitee * 【cron 】 修复Cron表达式range解析错误问题issue#I82CSH@Gitee
* 【core 】 修复VersionComparator在极端数据排序时候违反了自反性问题issue#I81N3H@Gitee * 【core 】 修复VersionComparator在极端数据排序时候违反了自反性问题issue#I81N3H@Gitee
* 【json 】 修复JSONStrFormatter:format函数对于转义符号处理逻辑错误问题issue#I84V6I@Gitee * 【json 】 修复JSONStrFormatter:format函数对于转义符号处理逻辑错误问题issue#I84V6I@Gitee
* 【core 】 修复特定情况下BiMap覆盖Value后仍能通过旧Value查询到Key问题issue#I88R5M@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.22(2023-09-13) # 5.8.22(2023-09-13)

View File

@ -30,7 +30,13 @@ public class BiMap<K, V> extends MapWrapper<K, V> {
@Override @Override
public V put(K key, V value) { public V put(K key, V value) {
final V oldValue = super.put(key, value);
if (null != this.inverse) { if (null != this.inverse) {
if(null != oldValue){
// issue#I88R5M
// 如果put的key相同value不同需要在inverse中移除旧的关联
this.inverse.remove(oldValue);
}
this.inverse.put(value, key); this.inverse.put(value, key);
} }
return super.put(key, value); return super.put(key, value);

View File

@ -0,0 +1,17 @@
package cn.hutool.core.map;
import org.junit.Assert;
import org.junit.Test;
import java.util.LinkedHashMap;
public class IssueI88R5MTest {
@Test
public void biMapTest() {
final BiMap<String, Integer> biMap = new BiMap<>(new LinkedHashMap<>());
biMap.put("aaa", 111);
biMap.getKey(111);
biMap.put("aaa", 222);
Assert.assertNull(biMap.getKey(111));
}
}