Java 把map的key转换成驼峰命名

需求:将map里的key转换为驼峰命名,并返回

 

package com.enation.app.javashop.framework.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * map工具类
 *
 * @author 
 * @date 2021/8/31 12:57
 */
public class MapUtils {

    /**
     * 把map的key转换成驼峰命名
     *
     * @param map
     * @return map
     */
    public static Map<String, Object> toReplaceKeyLow(Map<String, Object> map) {
        Map<String, Object> reMap = new HashMap<>();
        if (reMap != null) {
            Iterator var2 = map.entrySet().iterator();

            while (var2.hasNext()) {
                Entry<String, Object> entry = (Entry) var2.next();
                reMap.put(underlineToCamel((String) entry.getKey()), map.get(entry.getKey()));
            }

            map.clear();
        }

        return reMap;
    }

    public static final char UNDERLINE = ‘_‘;


    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(Character.toLowerCase(param.charAt(i)));
            }
        }
        return sb.toString();
    }


}

 

Java 把map的key转换成驼峰命名

上一篇:秦绪文:如何开展微信微博推广


下一篇:【C语言】第7章 用函数实现模块化程序设计