mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复BiMap中未重写computeIfAbsent和putIfAbsent导致双向查找出问题
This commit is contained in:
parent
4e06f02610
commit
d6134f707d
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.8.11.M1 (2022-11-24)
|
# 5.8.11.M1 (2022-11-26)
|
||||||
|
|
||||||
### 🐣新特性
|
### 🐣新特性
|
||||||
* 【core 】 CharUtil.isBlankChar增加\u180e(pr#2738@Github)
|
* 【core 】 CharUtil.isBlankChar增加\u180e(pr#2738@Github)
|
||||||
@ -11,6 +11,7 @@
|
|||||||
* 【json 】 修复普通byte数组转JSONArray时的异常(pr#875@Gitee)
|
* 【json 】 修复普通byte数组转JSONArray时的异常(pr#875@Gitee)
|
||||||
* 【core 】 修复ArrayUtil.insert()不支持原始类型数组的问题(pr#874@Gitee)
|
* 【core 】 修复ArrayUtil.insert()不支持原始类型数组的问题(pr#874@Gitee)
|
||||||
* 【core 】 修复HexUtil.isHexNumber()判断逻辑超出long的精度问题(issue#I62H7K@Gitee)
|
* 【core 】 修复HexUtil.isHexNumber()判断逻辑超出long的精度问题(issue#I62H7K@Gitee)
|
||||||
|
* 【core 】 修复BiMap中未重写computeIfAbsent和putIfAbsent导致双向查找出问题(issue#I62X8O@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package cn.hutool.core.map;
|
package cn.hutool.core.map;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 双向Map<br>
|
* 双向Map<br>
|
||||||
@ -45,7 +47,7 @@ public class BiMap<K, V> extends MapWrapper<K, V> {
|
|||||||
@Override
|
@Override
|
||||||
public V remove(Object key) {
|
public V remove(Object key) {
|
||||||
final V v = super.remove(key);
|
final V v = super.remove(key);
|
||||||
if(null != this.inverse && null != v){
|
if (null != this.inverse && null != v) {
|
||||||
this.inverse.remove(v);
|
this.inverse.remove(v);
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
@ -83,4 +85,49 @@ public class BiMap<K, V> extends MapWrapper<K, V> {
|
|||||||
public K getKey(V value) {
|
public K getKey(V value) {
|
||||||
return getInverse().get(value);
|
return getInverse().get(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V putIfAbsent(K key, V value) {
|
||||||
|
if (null != this.inverse) {
|
||||||
|
this.inverse.putIfAbsent(value, key);
|
||||||
|
}
|
||||||
|
return super.putIfAbsent(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||||
|
final V result = super.computeIfAbsent(key, mappingFunction);
|
||||||
|
resetInverseMap();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||||
|
final V result = super.computeIfPresent(key, remappingFunction);
|
||||||
|
resetInverseMap();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||||
|
final V result = super.compute(key, remappingFunction);
|
||||||
|
resetInverseMap();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
||||||
|
final V result = super.merge(key, value, remappingFunction);
|
||||||
|
resetInverseMap();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重置反转的Map,如果反转map为空,则不操作。
|
||||||
|
*/
|
||||||
|
private void resetInverseMap() {
|
||||||
|
if (null != this.inverse) {
|
||||||
|
inverse = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ public class BiMapTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getTest(){
|
public void getTest(){
|
||||||
BiMap<String, Integer> biMap = new BiMap<>(new HashMap<>());
|
final BiMap<String, Integer> biMap = new BiMap<>(new HashMap<>());
|
||||||
biMap.put("aaa", 111);
|
biMap.put("aaa", 111);
|
||||||
biMap.put("bbb", 222);
|
biMap.put("bbb", 222);
|
||||||
|
|
||||||
@ -19,4 +19,26 @@ public class BiMapTest {
|
|||||||
Assert.assertEquals("aaa", biMap.getKey(111));
|
Assert.assertEquals("aaa", biMap.getKey(111));
|
||||||
Assert.assertEquals("bbb", biMap.getKey(222));
|
Assert.assertEquals("bbb", biMap.getKey(222));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void computeIfAbsentTest(){
|
||||||
|
final BiMap<String, Integer> biMap = new BiMap<>(new HashMap<>());
|
||||||
|
biMap.put("aaa", 111);
|
||||||
|
biMap.put("bbb", 222);
|
||||||
|
|
||||||
|
biMap.computeIfAbsent("ccc", s -> 333);
|
||||||
|
Assert.assertEquals(new Integer(333), biMap.get("ccc"));
|
||||||
|
Assert.assertEquals("ccc", biMap.getKey(333));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void putIfAbsentTest(){
|
||||||
|
final BiMap<String, Integer> biMap = new BiMap<>(new HashMap<>());
|
||||||
|
biMap.put("aaa", 111);
|
||||||
|
biMap.put("bbb", 222);
|
||||||
|
|
||||||
|
biMap.putIfAbsent("ccc", 333);
|
||||||
|
Assert.assertEquals(new Integer(333), biMap.get("ccc"));
|
||||||
|
Assert.assertEquals("ccc", biMap.getKey(333));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user