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
阅读全文
0 0
- Spring--LinkedCaseInsensitiveMap
- Spring--LinkedCaseInsensitiveMap
- LinkedCaseInsensitiveMap的使用
- JDK1.8,LinkedCaseInsensitiveMap get方法返回null
- mybatis返回map key值大小写去重,CaseInsensitiveMap、LinkedCaseInsensitiveMap源码
- Spring
- Spring
- spring
- spring
- spring
- spring
- Spring
- spring
- spring
- Spring
- Spring
- spring
- Spring
- 海航集团王健出席“2016海航光明行”启动仪式
- 206. Reverse Linked List--链表翻转
- jquery post请求服务器资源
- 雅克比矩阵(Jacobian Matrix)的意义
- 高并发量网站解决方案
- Spring--LinkedCaseInsensitiveMap
- Python学习笔记(三)——List Comprehension 和 Generator Expression
- 自增主键的优缺点
- 结合代码彻底理解Spring AOP的术语
- 2017BNUZCPC
- iOS 设置圆角通用方法
- 使用 Office 365 PowerShell 管理用户帐户和许可证(二)使用 Office 365 PowerShell 查看授权和未授权的用户
- rex run 传参
- 38. Count and Say(第十二周)