Spring--LinkedCaseInsensitiveMap

来源:互联网 发布:windows u盘安装工具 编辑:程序博客网 时间:2024/06/08 03:14

在项目的配置文件中看到了LinkedCaseInsensitiveMap,查阅后了解到LinkedCaseInsensitiveMap继承于LinkedHashMap,LinkedCaseInsensitiveMap可以检测关键字(不区分大小写)的唯一性,源码如下:

package org.springframework.util;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Locale;import java.util.Map;import java.util.Map.Entry;public class LinkedCaseInsensitiveMap<V> extends LinkedHashMap<String, V> {    private Map<String, String> caseInsensitiveKeys;    private final Locale locale;    public LinkedCaseInsensitiveMap() {        this((Locale)null);    }    public LinkedCaseInsensitiveMap(Locale locale) {        this.caseInsensitiveKeys = new HashMap();        this.locale = locale != null?locale:Locale.getDefault();    }    public LinkedCaseInsensitiveMap(int initialCapacity) {        this(initialCapacity, (Locale)null);    }    public LinkedCaseInsensitiveMap(int initialCapacity, Locale locale) {        super(initialCapacity);        this.caseInsensitiveKeys = new HashMap(initialCapacity);        this.locale = locale != null?locale:Locale.getDefault();    }    public V put(String key, V value) {        String oldKey = (String)this.caseInsensitiveKeys.put(this.convertKey(key), key);        if(oldKey != null && !oldKey.equals(key)) {            super.remove(oldKey);        }        return super.put(key, value);    }    public void putAll(Map<? extends String, ? extends V> map) {        if(!map.isEmpty()) {            Iterator var2 = map.entrySet().iterator();            while(var2.hasNext()) {                Entry entry = (Entry)var2.next();                this.put((String)entry.getKey(), entry.getValue());            }        }    }    public boolean containsKey(Object key) {        return key instanceof String && this.caseInsensitiveKeys.containsKey(this.convertKey((String)key));    }    public V get(Object key) {        if(key instanceof String) {            String caseInsensitiveKey = (String)this.caseInsensitiveKeys.get(this.convertKey((String)key));            if(caseInsensitiveKey != null) {                return super.get(caseInsensitiveKey);            }        }        return null;    }    public V getOrDefault(Object key, V defaultValue) {        if(key instanceof String) {            String caseInsensitiveKey = (String)this.caseInsensitiveKeys.get(this.convertKey((String)key));            if(caseInsensitiveKey != null) {                return super.get(caseInsensitiveKey);            }        }        return defaultValue;    }    public V remove(Object key) {        if(key instanceof String) {            String caseInsensitiveKey = (String)this.caseInsensitiveKeys.remove(this.convertKey((String)key));            if(caseInsensitiveKey != null) {                return super.remove(caseInsensitiveKey);            }        }        return null;    }    public void clear() {        this.caseInsensitiveKeys.clear();        super.clear();    }    public Object clone() {        LinkedCaseInsensitiveMap copy = (LinkedCaseInsensitiveMap)super.clone();        copy.caseInsensitiveKeys = new HashMap(this.caseInsensitiveKeys);        return copy;    }    protected String convertKey(String key) {        return key.toLowerCase(this.locale);    }}

使用例子:

使用@ConfigurationProperties(“xx.xx.xx…”)注解注释类从配置文件中获取配置。

private Map<String, String> errorPages = new LinkedCaseInsensitiveMap<>();

从jdbcTemplate.queryForMap()追踪代码可以发现最后会进入ColumnMapRowMapper类的

public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {          ResultSetMetaData rsmd = rs.getMetaData();          int columnCount = rsmd.getColumnCount();          Map<String, Object> mapOfColValues = createColumnMap(columnCount);          for (int i = 1; i <= columnCount; i++) {              String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));              Object obj = getColumnValue(rs, i);              mapOfColValues.put(key, obj);          }          return mapOfColValues;      }  
protected Map<String, Object> createColumnMap(int columnCount) {          return new LinkedCaseInsensitiveMap<Object>(columnCount);      }  

自己做了个小测试:

public static void main(String[] args) {        Map<String,Object>  lciMap=new LinkedCaseInsensitiveMap();        lciMap.put("ABCDEF","1");        System.out.println(lciMap.containsKey("abcdef"));        lciMap.put("abcdef","2");        System.out.println(lciMap.containsKey("abcdef"));        System.out.println(lciMap.get("abcdef"));    }

运行结果:

truetrue2
原创粉丝点击