java 不区分KEY大小写的MAP
有时候我们会需要一个不区分KEY大小写的MAP,
今天介绍一个apache.commons包里的类,
org.apache.commons.collections.map.CaseInsensitiveMap
例子:
public static void main(String[] args) {
Map<String, Object> result = new CaseInsensitiveMap();
result.put("aaa", "ok");
System.out.println(result.get("aaa"));
System.out.println(result.get("aAa"));
System.out.println(result.get("AAa"));
System.out.println(result.get("AAA"));
}
输出:
ok
ok
ok
ok
大家用到的时候记得。
HashMap是要区分大小写的
public static void main(String[] args) {
Map<String, String> result = new HashMap<String, String>();
result.put("aaa", "ok");
System.out.println(result.get("aaa"));
System.out.println(result.get("aAa"));
System.out.println(result.get("AAa"));
System.out.println(result.get("AAA"));
}
ok
null
null
null