业务场景:
存key和取key时忽略大小写
package com.dhcc.dqc.config;
import java.util.HashMap;
import java.util.Map;
/**
* 忽略大小写的Map
* @author zhang
*/
public class CaseInsensitiveMap extends HashMap<String, Object> {
private final Map<String, String> keys = new HashMap<>();
public CaseInsensitiveMap(Map<String, Object> map2) {
for (String s : map2.keySet()) {
this.keys.put(s.toLowerCase(), s);
}
this.putAll(map2);
}
public Object getValue(String key) {
String value = keys.get(key.toLowerCase());
return get(value);
}
@Override
public Object put(String key, Object value) {
this.keys.put(key.toLowerCase(), key);
return super.put(key, value);
}
@Override
public boolean containsKey(Object key) {
if (key instanceof String){
return super.containsKey(((String) key).toLowerCase());
}
return super.containsKey(key);
}
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("AAAa", 1);
map.put("bbb", "www");
CaseInsensitiveMap ciMap = new CaseInsensitiveMap(map);
ciMap.put("aaaa", 2);
System.out.println(ciMap.containsKey("AaaA"));
System.out.println(ciMap.getValue("aAAa"));
System.out.println(ciMap.getValue("AAAA"));
}
}