CaseInsensitiveMap—不区分大小写的无序map;
LinkedCaseInsensitiveMap—不区分大小写的有序map;
数据库本身对大小写不敏感(我使用的是mysql),但是创建表格的时候,数据库里面字段都会默认小写,所以MyBatis映射的时候,key也会映射成小写,可以用LinkedCaseInsensitiveMap(key值不区分大小写的LinkedMap)来处理。
LinkedCaseInsensitiveMap属于spring,要注意导包
org.springframework.util.LinkedCaseInsensitiveMap.class
LinkedCaseInsensitiveMap 继承了 LinkedHashMap,可以检测关键字(不区分大小写)的唯一性
源码解析:
package org.springframework.util;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.springframework.lang.Nullable;
@SuppressWarnings("serial")
public class LinkedCaseInsensitiveMap<V> implements Map<String, V>, Serializable, Cloneable {
private final LinkedHashMap<String, V> targetMap;
private final HashMap<String, String> caseInsensitiveKeys;
private final Locale locale;
public LinkedCaseInsensitiveMap() {
this((Locale) null);
}
public LinkedCaseInsensitiveMap(@Nullable Locale locale) {
this(16, locale);
}
public LinkedCaseInsensitiveMap(int initialCapacity) {
this(initialCapacity, null);
}
public LinkedCaseInsensitiveMap(int initialCapacity, @Nullable Locale locale) {
this.targetMap = new LinkedHashMap<String, V>(initialCapacity) {
@Override
public boolean containsKey(Object key) {
return LinkedCaseInsensitiveMap.this.containsKey(key);
}
@Override
protected boolean removeEldestEntry(Map.Entry<String, V> eldest) {
boolean doRemove = LinkedCaseInsensitiveMap.this.removeEldestEntry(eldest);
if (doRemove) {
caseInsensitiveKeys.remove(convertKey(eldest.getKey()));
}
return doRemove;
}
};
this.caseInsensitiveKeys = new HashMap<>(initialCapacity);
this.locale = (locale != null ? locale : Locale.getDefault());
}
/**
* Copy constructor.
*/
@SuppressWarnings("unchecked")
private LinkedCaseInsensitiveMap(LinkedCaseInsensitiveMap<V> other) {
this.targetMap = (LinkedHashMap<String, V>) other.targetMap.clone();
this.caseInsensitiveKeys = (HashMap<String, String>) other.caseInsensitiveKeys.clone();
this.locale = other.locale;
}
// Implementation of java.util.Map
@Override
public int size() {
return this.targetMap.size();
}
@Override
public boolean isEmpty() {
return this.targetMap.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return (key instanceof String && this.caseInsensitiveKeys.containsKey(convertKey((String) key)));
}
@Override
public boolean containsValue(Object value) {
return this.targetMap.containsValue(value);
}
@Override
@Nullable
public V get(Object key) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return null;
}
@Override
public V getOrDefault(Object key, V defaultValue) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.get(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.get(caseInsensitiveKey);
}
}
return defaultValue;
}
@Override
public V put(String key, @Nullable V value) {
String oldKey = this.caseInsensitiveKeys.put(convertKey(key), key);
if (oldKey != null && !oldKey.equals(key)) {
this.targetMap.remove(oldKey);
}
return this.targetMap.put(key, value);
}
@Override
public void putAll(Map<? extends String, ? extends V> map) {
if (map.isEmpty()) {
return;
}
map.forEach(this::put);
}
@Override
@Nullable
public V remove(Object key) {
if (key instanceof String) {
String caseInsensitiveKey = this.caseInsensitiveKeys.remove(convertKey((String) key));
if (caseInsensitiveKey != null) {
return this.targetMap.remove(caseInsensitiveKey);
}
}
return null;
}
@Override
public void clear() {
this.caseInsensitiveKeys.clear();
this.targetMap.clear();
}
@Override
public Set<String> keySet() {
return this.targetMap.keySet();
}
@Override
public Collection<V> values() {
return this.targetMap.values();
}
@Override
public Set<Entry<String, V>> entrySet() {
return this.targetMap.entrySet();
}
@Override
public LinkedCaseInsensitiveMap<V> clone() {
return new LinkedCaseInsensitiveMap<>(this);
}
@Override
public boolean equals(Object obj) {
return this.targetMap.equals(obj);
}
@Override
public int hashCode() {
return this.targetMap.hashCode();
}
@Override
public String toString() {
return this.targetMap.toString();
}
// Specific to LinkedCaseInsensitiveMap
public Locale getLocale() {
return this.locale;
}
protected String convertKey(String key) {
return key.toLowerCase(getLocale());
}
protected boolean removeEldestEntry(Map.Entry<String, V> eldest) {
return false;
}
}