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

java

需求:将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转换成驼峰命名 的全部内容, 来源链接: utcz.com/z/392292.html

回到顶部