修复CaseInsensitiveLinkedMap顺序错误问题

This commit is contained in:
Looly 2024-06-12 15:30:06 +08:00
parent 3aa9bdfcfa
commit afe34a783d
5 changed files with 51 additions and 10 deletions

View File

@ -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)

View File

@ -61,7 +61,7 @@ public class CaseInsensitiveLinkedMap<K, V> extends CaseInsensitiveMap<K, V> {
* @param loadFactor 加载因子
*/
public CaseInsensitiveLinkedMap(int initialCapacity, float loadFactor) {
super(new LinkedHashMap<>(initialCapacity, loadFactor));
super(MapBuilder.create(new LinkedHashMap<>(initialCapacity, loadFactor)));
}
// ------------------------------------------------------------------------- Constructor end
}

View File

@ -37,7 +37,7 @@ public class CaseInsensitiveMap<K, V> extends FuncKeyMap<K, V> {
/**
* 构造<br>
* 注意此构造将传入的Map作为被包装的Map针对任何修改传入的Map都会被同样修改
* 注意此构造将传入的Map所有值复制到当前map中不修改传入map
*
* @param m 被包装的自定义Map创建器
*/

View File

@ -44,7 +44,7 @@ public class CaseInsensitiveTreeMap<K, V> extends CaseInsensitiveMap<K, V> {
* @since 3.1.2
*/
public CaseInsensitiveTreeMap(SortedMap<? extends K, ? extends V> m) {
super(new TreeMap<K, V>(m));
super(MapBuilder.create(new TreeMap<K, V>(m)));
}
/**
@ -53,7 +53,7 @@ public class CaseInsensitiveTreeMap<K, V> extends CaseInsensitiveMap<K, V> {
* @param comparator 比较器{@code null}表示使用默认比较器
*/
public CaseInsensitiveTreeMap(Comparator<? super K> comparator) {
super(new TreeMap<>(comparator));
super(MapBuilder.create(new TreeMap<>(comparator)));
}
// ------------------------------------------------------------------------- Constructor end
}

View File

@ -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<String, String> 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<String, String> 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<String, Object> 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<String, Object> 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();
});
}
}