Java中對於鍵值對,咱們習慣使用類HashMap,使用方式:Map<String, String> result=new HashMap<String,String>();ide
可是這種使用的鍵Key是區分大小寫的,也就是說Name和name會被視爲不一樣的鍵Key。若是咱們須要不區分大小寫。那麼就要使用類CaseInsensitiveMap。spa
使用方式,首先引用jar包,pom.xml:code
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency>
而後Java的示例代碼以下:xml
public static void main(String[] args) { //Map<String, String> result=new HashMap<String,String>(); Map<String, String> result = new CaseInsensitiveMap(); result.put("name", "zhangsan"); result.put("Name", "lisi"); System.out.println(result.get("name")); System.out.println(result.get("nAme")); }
輸出的結果就是:blog
lisiget
lisiit
很是簡單,可是注意包名。io
//-----------------------------------------------------------------------
/**
* Overrides convertKey() from {@link AbstractHashedMap} to convert keys to
* lower case.
* <p>
* Returns {@link AbstractHashedMap#NULL} if key is null.
*
* @param key the key convert
* @return the converted key
*/
protected Object convertKey(Object key) {
if (key != null) {
char[] chars = key.toString().toCharArray();
for (int i = chars.length - 1; i >= 0; i--) {
chars[i] = Character.toLowerCase(Character.toUpperCase(chars[i]));
}
return new String(chars);
} else {
return AbstractHashedMap.NULL;
}
}class