Java:将map集合中的key键转换成小写、大写方法


     // main方法运行测试

     public static void main(String[] args) {
 
        Map<String, Object> m3 = new HashMap<String, Object>();
        m3.put("a", "abc");
        m3.put("b", "123");
        m3.put("C", "123");
        m3.put("dD", "123");
 
        Map<String, Object> m4 = new HashMap<String, Object>();
        m4.put("b", "123");
        m4.put("a", "abc");
        m4.put("cDs", "123");
        m4.put("d", "123");
        
        System.out.println("将m4数据的key全部转换为大写===" + transformUpperCase(m4));
        System.out.println("将m3数据的key全部转换为小写===" + transformLowerCase(m4));
 
    }
 
    // 将map值全部转换为大写
    public static Map<String, Object> transformUpperCase(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<>();
        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }
        Set<String> keySet = orgMap.keySet();
        for (String key : keySet) {
            String newKey = key.toUpperCase();
//            newKey = newKey.replace("_", "");
            resultMap.put(newKey, orgMap.get(key));
        }
        return resultMap;
    }
 
    // 将map值全部转换为小写
    public static Map<String, Object> transformLowerCase(Map<String, Object> orgMap) {
        Map<String, Object> resultMap = new HashMap<>();
        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }
        Set<String> keySet = orgMap.keySet();
        for (String key : keySet) {
            String newKey = key.toLowerCase();
//            newKey = newKey.replace("_", "");
            resultMap.put(newKey, orgMap.get(key));
        }
        return resultMap;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章