mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复特定情况下BiMap覆盖Value后,仍能通过旧Value查询到Key问题
This commit is contained in:
@@ -30,7 +30,13 @@ public class BiMap<K, V> extends MapWrapper<K, V> {
|
||||
|
||||
@Override
|
||||
public V put(K key, V value) {
|
||||
final V oldValue = super.put(key, value);
|
||||
if (null != this.inverse) {
|
||||
if(null != oldValue){
|
||||
// issue#I88R5M
|
||||
// 如果put的key相同,value不同,需要在inverse中移除旧的关联
|
||||
this.inverse.remove(oldValue);
|
||||
}
|
||||
this.inverse.put(value, key);
|
||||
}
|
||||
return super.put(key, value);
|
||||
|
@@ -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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user