From afe34a783d27f6fd145347ff95eccc16c60c090e Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 12 Jun 2024 15:30:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DCaseInsensitiveLinkedMap?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + .../core/map/CaseInsensitiveLinkedMap.java | 2 +- .../hutool/core/map/CaseInsensitiveMap.java | 2 +- .../core/map/CaseInsensitiveTreeMap.java | 4 +- .../core/map/CaseInsensitiveMapTest.java | 52 ++++++++++++++++--- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d071a09c..fbb7d4d3e 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * 【core 】 修复AnnotationUtil可能的空指针错误 * 【core 】 修复BeanUtil.isBean判断Dict错误问题(issue#I9VTZG@Gitee) * 【core 】 修复VersionComparator传入空字符串报错问题(pr#3614@Github) +* 【core 】 修复CaseInsensitiveLinkedMap顺序错误问题(issue#IA4K4F@Gitee) ------------------------------------------------------------------------------------------------------------- # 5.8.28(2024-05-29) diff --git a/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveLinkedMap.java b/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveLinkedMap.java index 5be588ef3..13e39eb27 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveLinkedMap.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveLinkedMap.java @@ -61,7 +61,7 @@ public class CaseInsensitiveLinkedMap extends CaseInsensitiveMap { * @param loadFactor 加载因子 */ public CaseInsensitiveLinkedMap(int initialCapacity, float loadFactor) { - super(new LinkedHashMap<>(initialCapacity, loadFactor)); + super(MapBuilder.create(new LinkedHashMap<>(initialCapacity, loadFactor))); } // ------------------------------------------------------------------------- Constructor end } diff --git a/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveMap.java b/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveMap.java index 23ff56412..b451c8265 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveMap.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveMap.java @@ -37,7 +37,7 @@ public class CaseInsensitiveMap extends FuncKeyMap { /** * 构造
- * 注意此构造将传入的Map作为被包装的Map,针对任何修改,传入的Map都会被同样修改。 + * 注意此构造将传入的Map所有值复制到当前map中,不修改传入map。 * * @param m 被包装的自定义Map创建器 */ diff --git a/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveTreeMap.java b/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveTreeMap.java index 69f3ec538..3a24f26c9 100755 --- a/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveTreeMap.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/CaseInsensitiveTreeMap.java @@ -44,7 +44,7 @@ public class CaseInsensitiveTreeMap extends CaseInsensitiveMap { * @since 3.1.2 */ public CaseInsensitiveTreeMap(SortedMap m) { - super(new TreeMap(m)); + super(MapBuilder.create(new TreeMap(m))); } /** @@ -53,7 +53,7 @@ public class CaseInsensitiveTreeMap extends CaseInsensitiveMap { * @param comparator 比较器,{@code null}表示使用默认比较器 */ public CaseInsensitiveTreeMap(Comparator comparator) { - super(new TreeMap<>(comparator)); + super(MapBuilder.create(new TreeMap<>(comparator))); } // ------------------------------------------------------------------------- Constructor end } diff --git a/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java b/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java index a62372f12..63fafea4d 100644 --- a/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/map/CaseInsensitiveMapTest.java @@ -1,25 +1,29 @@ package cn.hutool.core.map; import cn.hutool.core.lang.Pair; -import org.junit.Assert; import org.junit.Test; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertEquals; + public class CaseInsensitiveMapTest { @Test public void caseInsensitiveMapTest() { CaseInsensitiveMap map = new CaseInsensitiveMap<>(); map.put("aAA", "OK"); - Assert.assertEquals("OK", map.get("aaa")); - Assert.assertEquals("OK", map.get("AAA")); + assertEquals("OK", map.get("aaa")); + assertEquals("OK", map.get("AAA")); } @Test public void caseInsensitiveLinkedMapTest() { CaseInsensitiveLinkedMap map = new CaseInsensitiveLinkedMap<>(); map.put("aAA", "OK"); - Assert.assertEquals("OK", map.get("aaa")); - Assert.assertEquals("OK", map.get("AAA")); + assertEquals("OK", map.get("aaa")); + assertEquals("OK", map.get("AAA")); } @Test @@ -31,6 +35,42 @@ public class CaseInsensitiveMapTest { map.merge(b.getKey(), b.getValue(), (A, B) -> A); map.merge(a.getKey(), a.getValue(), (A, B) -> A); - Assert.assertEquals(1, map.size()); + assertEquals(1, map.size()); + } + + @Test + public void issueIA4K4FTest() { + Map map = new CaseInsensitiveLinkedMap<>(); + map.put("b", 2); + map.put("a", 1); + + AtomicInteger index = new AtomicInteger(); + map.forEach((k, v) -> { + if(0 == index.get()){ + assertEquals("b", k); + } else if(1 == index.get()){ + assertEquals("a", k); + } + + index.getAndIncrement(); + }); + } + + @Test + public void issueIA4K4FTest2() { + Map map = new CaseInsensitiveTreeMap<>(); + map.put("b", 2); + map.put("a", 1); + + AtomicInteger index = new AtomicInteger(); + map.forEach((k, v) -> { + if(0 == index.get()){ + assertEquals("a", k); + } else if(1 == index.get()){ + assertEquals("b", k); + } + + index.getAndIncrement(); + }); } }