Java 有没有一种好方法可以让 Map<String, ?> get 和 put 忽略大小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:26:54  来源:igfitidea点击:

Is there a good way to have a Map<String, ?> get and put ignoring case?

javamapcase-insensitive

提问by Joshua

Is there a good way to have a Map<String, ?>get and put ignoring case?

有没有一个很好的方法来Map<String, ?>获取和放置忽略大小写?

采纳答案by volley

TreeMap extends Map and supports custom comparators.

TreeMap 扩展了 Map 并支持自定义比较器。

String provides a default case insensitive comparator.

String 提供了一个默认的不区分大小写的比较器。

So:

所以:

final Map<String, ...> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);

The comparator does not take locale into account. Read more about it in its JavaDoc.

比较器不考虑区域设置。在它的 JavaDoc 中阅读更多关于它的信息。

回答by John M

You need a wrapper class for your String key with a case-insensitive equals() and hashCode() implementation. Use that instead of the String for the Map's key.

您需要一个 String 键的包装类,其中包含不区分大小写的 equals() 和 hashCode() 实现。使用它而不是 String 作为 Map 的键。

See an example implementation at http://www.java.happycodings.com/Java_Util_Package/code3.htmlI found it in 2 minutes of googling. Looks sensible to me, though I've never used it.

http://www.java.happycodings.com/Java_Util_Package/code3.html查看示例实现 我在 2 分钟的谷歌搜索中找到了它。对我来说看起来很明智,尽管我从未使用过它。

回答by Eric Weilnau

You could use CaseInsensitiveMapfrom Apache's Commons Collections.

您可以使用Apache 的 Commons Collections 中的CaseInsensitiveMap

回答by Guido

Would it be possible to implement your own Map overriding put/get methods ?

是否可以实现您自己的 Map 覆盖 put/get 方法?

public class CaseInsensitiveMap extends HashMap<String, String> {
    ...
    put(String key, String value) {
       super.put(key.toLowerCase(), value);
    }

    get(String key) {
       super.get(key.toLowercase());
    }
}

This approach does not force you to change your "key" type but your Map implementation.

这种方法不会强迫您更改“键”类型,而是您的 Map 实现。

回答by Tom Hawtin - tackline

The three obvious solutions that spring to mind:

三个显而易见的解决方案:

  • Normalise the case before using a String as a key (not Turkish locale works differently from the rest of the world).

  • Use a special object type designed to be used as key. This is a common idiom for dealing with composite keys.

  • Use a TreeMap with a Comparator that is case insensitive (possibly a PRIMARY or SECONDARY strength java.text.Collator). Unfortunately the Java library doesn't have a Comparator equivalent for hashCode/equals.

  • 在使用字符串作为键之前规范化大小写(不是土耳其语言环境与世界其他地方的工作方式不同)。

  • 使用专门用作键的特殊对象类型。这是处理复合键的常用习惯用法。

  • 将 TreeMap 与不区分大小写的 Comparator(可能是 PRIMARY 或 SECONDARY 强度 java.text.Collat​​or)一起使用。不幸的是,Java 库没有与 hashCode/equals 等效的 Comparator。

回答by Glyn Normington

You could use my Apache licensed CaseInsensitiveMapdiscussed here. Unlike the Apache Commons version, it preserves the case of keys. It implements the map contract more strictly than TreeMap (plus has better concurrent semantics) (see the blog comments for details).

您可以使用此处讨论的我的 Apache 许可CaseInsensitiveMap。与 Apache Commons 版本不同,它保留了键的大小写。它比 TreeMap 更严格地实现了地图契约(加上具有更好的并发语义)(详细信息请参阅博客评论)。

回答by volley

Trove4jcan use custom hashing for a HashMap. This may however have performance implications given that hashcodes cannot be cached (although Trove4j may have found a way around this?). Wrapper objects (as described by John M) do not have this caching deficiency. Also see my other answer regarding TreeMap.

Trove4j可以对 HashMap 使用自定义散列。然而,鉴于无法缓存哈希码,这可能会对性能产生影响(尽管 Trove4j 可能已经找到了解决此问题的方法?)。包装对象(如 John M 所述)没有这种缓存缺陷。另请参阅我关于 TreeMap 的其他答案。

回答by Kunjan

Check the accepted answer at the link below. How to check for key in a Map irrespective of the case?

在下面的链接中检查接受的答案。 无论情况如何,如何检查地图中的键?

Bottom line is "The simplest solution is to simply convert all inputs to uppercase (or lowercase) before inserting/checking. You could even write your own Map wrapper that would do this to ensure consistency."

底线是“最简单的解决方案是在插入/检查之前简单地将所有输入转换为大写(或小写)。您甚至可以编写自己的 Map 包装器来确保一致性。”