转换驼峰式命名

package com.tian.tools.utils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: 吴昊天
 * @date: 2020/6/23 16:40
 * @description: 转换驼峰式命名
 */
public class SwitchHump {
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * @Description: 将map的Key大写转换成小写
     * @Param: list
     * @return:
     * @Author: 吴昊天
     * @Date: 2020/6/23 16:40
     */
    public static List<Map<String, Object>> upperCaseToLowerCase(List<Map<String, Object>> list) {

        List<Map<String, Object>> objects = new ArrayList<>();
        try {
            for (Map<String, Object> map : list) {
                Map<String, Object> stringObjectHashMap = new HashMap<>();
                for (String oldKey : map.keySet()) {
                    //key是旧的key值
                    String newKey = oldKey.toLowerCase();
                    //调用lineToHump方法,将下划线转成驼峰式
                    String s = lineToHump(newKey);
                    stringObjectHashMap.put(s, map.get(oldKey));

                }
                objects.add(stringObjectHashMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return objects;
    }

    /**
     * @Description: 下划线转驼峰
     * @Param:
     * @return:
     * @Author: 吴昊天
     * @Date: 2020/6/23 16:55
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}

猜你喜欢

转载自blog.csdn.net/wutian842929/article/details/106926860