mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:20:07 +08:00
修复TableMap没有default方法导致的问题
This commit is contained in:
parent
f6fe00aa69
commit
92003ecbfb
@ -3,7 +3,7 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.8.9.M1 (2022-10-16)
|
||||
# 5.8.9.M1 (2022-10-22)
|
||||
|
||||
### 🐣新特性
|
||||
* 【core 】 DateUtil增加isLastDayOfMonth、getLastDayOfMonth方法(pr#824@Gitee)
|
||||
@ -24,6 +24,7 @@
|
||||
* 【cache 】 修复LRUCache移除事件监听失效问题(issue#2647@Github)
|
||||
* 【core 】 修复MapToMap中ignoreNullValue无效问题(issue#2647@Github)
|
||||
* 【core 】 修复ReflectUtil.invokeRaw方法转换失败抛出异常问题(pr#837@Gitee)
|
||||
* 【core 】 修复TableMap没有default方法导致的问题(issue#I5WMST@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -2,6 +2,7 @@ package cn.hutool.core.map;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
@ -14,6 +15,8 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
/**
|
||||
* 可重复键和值的Map<br>
|
||||
@ -86,7 +89,7 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
public V get(Object key) {
|
||||
//noinspection SuspiciousMethodCalls
|
||||
final int index = keys.indexOf(key);
|
||||
if (index > -1 && index < values.size()) {
|
||||
if (index > -1) {
|
||||
return values.get(index);
|
||||
}
|
||||
return null;
|
||||
@ -101,7 +104,7 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
*/
|
||||
public K getKey(V value) {
|
||||
final int index = values.indexOf(value);
|
||||
if (index > -1 && index < keys.size()) {
|
||||
if (index > -1) {
|
||||
return keys.get(index);
|
||||
}
|
||||
return null;
|
||||
@ -142,17 +145,32 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除指定的所有键和对应的所有值
|
||||
*
|
||||
* @param key 键
|
||||
* @return 最后一个移除的值
|
||||
*/
|
||||
@Override
|
||||
public V remove(Object key) {
|
||||
V lastValue = null;
|
||||
int index;
|
||||
//noinspection SuspiciousMethodCalls
|
||||
int index = keys.indexOf(key);
|
||||
if (index > -1) {
|
||||
keys.remove(index);
|
||||
if (index < values.size()) {
|
||||
values.remove(index);
|
||||
}
|
||||
while ((index = keys.indexOf(key)) > -1) {
|
||||
lastValue = removeByIndex(index);
|
||||
}
|
||||
return null;
|
||||
return lastValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除指定位置的键值对
|
||||
*
|
||||
* @param index 位置,不能越界
|
||||
* @return 移除的值
|
||||
*/
|
||||
public V removeByIndex(final int index) {
|
||||
keys.remove(index);
|
||||
return values.remove(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -228,4 +246,85 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
", values=" + values +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(final BiConsumer<? super K, ? super V> action) {
|
||||
for (int i = 0; i < size(); i++) {
|
||||
action.accept(keys.get(i), values.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(final Object key, final Object value) {
|
||||
boolean removed = false;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (ObjUtil.equals(key, keys.get(i)) && ObjUtil.equals(value, values.get(i))) {
|
||||
removeByIndex(i);
|
||||
removed = true;
|
||||
// 移除当前元素,下个元素前移
|
||||
i--;
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAll(final BiFunction<? super K, ? super V, ? extends V> function) {
|
||||
for (int i = 0; i < size(); i++) {
|
||||
final V newValue = function.apply(keys.get(i), values.get(i));
|
||||
values.set(i, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean replace(final K key, final V oldValue, final V newValue) {
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (ObjUtil.equals(key, keys.get(i)) && ObjUtil.equals(oldValue, values.get(i))) {
|
||||
values.set(i, newValue);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 替换指定key的所有值为指定值
|
||||
*
|
||||
* @param key 指定的key
|
||||
* @param value 替换的值
|
||||
* @return 最后替换的值
|
||||
*/
|
||||
@Override
|
||||
public V replace(final K key, final V value) {
|
||||
V lastValue = null;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (ObjUtil.equals(key, keys.get(i))) {
|
||||
lastValue = values.set(i, value);
|
||||
}
|
||||
}
|
||||
return lastValue;
|
||||
}
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||
if(null == remappingFunction){
|
||||
return null;
|
||||
}
|
||||
|
||||
V lastValue = null;
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (ObjUtil.equals(key, keys.get(i))) {
|
||||
final V newValue = remappingFunction.apply(key, values.get(i));
|
||||
if(null != newValue){
|
||||
lastValue = values.set(i, newValue);
|
||||
} else{
|
||||
removeByIndex(i);
|
||||
// 移除当前元素,下个元素前移
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return lastValue;
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ public class TableMapTest {
|
||||
|
||||
@Test
|
||||
public void getTest(){
|
||||
TableMap<String, Integer> tableMap = new TableMap<>(16);
|
||||
final TableMap<String, Integer> tableMap = new TableMap<>(16);
|
||||
tableMap.put("aaa", 111);
|
||||
tableMap.put("bbb", 222);
|
||||
|
||||
@ -17,4 +17,30 @@ public class TableMapTest {
|
||||
Assert.assertEquals("aaa", tableMap.getKey(111));
|
||||
Assert.assertEquals("bbb", tableMap.getKey(222));
|
||||
}
|
||||
|
||||
@SuppressWarnings("OverwrittenKey")
|
||||
@Test
|
||||
public void removeTest() {
|
||||
final TableMap<String, Integer> tableMap = new TableMap<>(16);
|
||||
tableMap.put("a", 111);
|
||||
tableMap.put("a", 222);
|
||||
tableMap.put("a", 222);
|
||||
|
||||
tableMap.remove("a");
|
||||
|
||||
Assert.assertEquals(0, tableMap.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("OverwrittenKey")
|
||||
@Test
|
||||
public void removeTest2() {
|
||||
final TableMap<String, Integer> tableMap = new TableMap<>(16);
|
||||
tableMap.put("a", 111);
|
||||
tableMap.put("a", 222);
|
||||
tableMap.put("a", 222);
|
||||
|
||||
tableMap.remove("a", 222);
|
||||
|
||||
Assert.assertEquals(1, tableMap.size());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user