mybatis返回map key值大小写去重,CaseInsensitiveMap、LinkedCaseInsensitiveMap源码


今天在写项目的时候遇见一个问题:

编写的是一套完全解耦的模块,用于利用freemarker模板动态拼接sql

然而拼接好的sql只能用LinkedHashMap返回结果集,保证数据有序,但是在数据输出的时候,mybatis 返回了key 相同,但大小写不同的数据,

在处理这个数据的时候,首先我选用了利用value值相同去做处理,但是有些数据的value值相同但是key不同

看来只能用key去做处理,首先我用了CaseInsensitiveMap 将linkedHashMap的数据放入到CaseInsensitiveMap 中,

输出的时候结果确实起到了去重的效果,但是在excel导出的时候要对应header标题头,mybatis 查询的顺序跟header 抬头是对应的,但是取出的顺序却不是对应的,

因此只能通过排序进行数据对应,但是排序的话只能使用LinkedHashMap去记住顺序

因此查询了资料发现了LinkedCaseInsensitiveMap

LinkedCaseInsensitiveMap 继承了 LinkedHashMap,可以检测关键字(不区分大小写)的唯一性,所以 ok  bug完美解决

附加 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);
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值