gpt4 book ai didi

java - 为什么 LinkedCaseInsensitiveMap 同时使用 LinkedHashMap 和 HashMap?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:31:04 25 4
gpt4 key购买 nike

我正在查看 LinkedCaseInsensitiveMap 的结构(spring 框架 5.0.5.RELEASE)。我很好奇为什么 LinkedCaseInsensitiveMap 同时使用 LinkedHashMap 和 HashMap,为什么不直接使用 LinkedHashMap?

   private final LinkedHashMap<String, V> targetMap;

public V get(Object key) {
if (key instanceof String) {
return this.targetMap.get(convertKey((String) key));
}
return null;
}

最佳答案

private final LinkedHashMap<String, V> targetMap;

private final HashMap<String, String> caseInsensitiveKeys;

在这种情况下,targetMap 包含您的对象的真实大小写字符串,caseInsensitiveKeys 包含将您的小写 key 映射到您的真实大小写 key 。

它允许在每次迭代时向您显示实际大小写键,但同时允许您不区分大小写。

让我们说,下面的代码:

LinkedCaseInsensitiveMap<Object> map = new LinkedCaseInsensitiveMap<>();
map.put("MyCustomObject", new Object());

"MyCustomObject"-> new Object() 放入 targetMap,将 "mycustomobject"-> "MyCustomObject" 放入 caseInsensitiveKeys。现在,如果您尝试打印 map 中的所有对象,它会在您添加而不是更改键时打印它。没有第二张 map 就无法存档。

关于java - 为什么 LinkedCaseInsensitiveMap 同时使用 LinkedHashMap 和 HashMap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55728741/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com