如何对Map集合的key进行大小写转换?

工具类:

ToUpperCaseKeyMapUtil.java

public class ToUpperCaseKeyMapUtil {

    //对单一的map
    public static  Map toUpperCaseKeyMap(Map map) {
        if (map != null) {
            List mapKeyList = new ArrayList<>(map.keySet());
            mapKeyList.forEach(key->{
                map.put(key.toUpperCase(),map.remove(key));//字段名转大写
            });
        }
        return map;
    }

    //对不唯一的map集合进行转换
    public static  List> toUpperCaseKeyMapList(List> mapList) {
        if (mapList != null && mapList.size() > 0) {
            mapList.forEach(ToUpperCaseKeyMapUtil::toUpperCaseKeyMap);
        }
        return mapList;
    }


  /**
     * key转小写
     *
     * @param orgMap
     * @return
     */
    public static Map keyToLowerCase(Map orgMap) {
        Map resultMap = new HashMap<>();

        if (orgMap == null || orgMap.isEmpty()) {
            return resultMap;
        }

        Set> entrySet = orgMap.entrySet();
        for (Map.Entry entry : entrySet) {
            String key = entry.getKey();
            Object value = entry.getValue();
            resultMap.put(key.toLowerCase(), value);
        }

        return resultMap;
    }

}

使用:

   public static void main(String[] args) {
        HashMap map = new HashMap<>();
        map.put("ksd","999");
        map.put("ads","999");
        HashMap map1 = new HashMap<>();
        map1.put("ew","23123");
        Map upperCaseKeyMap = toUpperCaseKeyMap(map);
        Set strings = upperCaseKeyMap.keySet();
        System.out.println(strings);
        System.out.println("===========================================");
        List> list = new ArrayList<>();
        list.add(map);
        list.add(map1);
        List> mapList = toUpperCaseKeyMapList(list);
        mapList.stream().forEach(stringObjectMap -> System.out.println(stringObjectMap));
    }

你可能感兴趣的:(java,开发语言)