From 92003ecbfba891c081005b744a4ce31f8369be80 Mon Sep 17 00:00:00 2001 From: Looly Date: Sat, 22 Oct 2022 22:11:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DTableMap=E6=B2=A1=E6=9C=89def?= =?UTF-8?q?ault=E6=96=B9=E6=B3=95=E5=AF=BC=E8=87=B4=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../java/cn/hutool/core/map/TableMap.java | 117 ++++++++++++++++-- .../java/cn/hutool/core/map/TableMapTest.java | 28 ++++- 3 files changed, 137 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75f4991d1..1d24cac2e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java b/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java index 326637a0a..7fd4eb992 100755 --- a/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/TableMap.java @@ -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
@@ -86,7 +89,7 @@ public class TableMap implements Map, Iterable>, 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 implements Map, Iterable>, 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 implements Map, Iterable>, 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 implements Map, Iterable>, Ser ", values=" + values + '}'; } + + @Override + public void forEach(final BiConsumer 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 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 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; + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/map/TableMapTest.java b/hutool-core/src/test/java/cn/hutool/core/map/TableMapTest.java index c8aba0f53..f13a720ea 100644 --- a/hutool-core/src/test/java/cn/hutool/core/map/TableMapTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/map/TableMapTest.java @@ -7,7 +7,7 @@ public class TableMapTest { @Test public void getTest(){ - TableMap tableMap = new TableMap<>(16); + final TableMap 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 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 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()); + } }