package com.zhuzher.test;
import com.zhuzher.common.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author peng2.li(李鹏1204630)
* @date 2022/1/22 15:14
*/
public class Test2 {
public static void main(String[] args) {
Map<String, Object> map = new HashMap<>();
map.put("1_app_a", "a");
map.put("2_app_b", "b");
map.put("3_app_c", "c");
map.put("4_app_d", "d");
Map<String, Object> map1 = getMap(map);
for (Map.Entry<String, Object> entry : map1.entrySet()) {
System.out.println("key=" + entry.getKey() + " value=" + entry.getValue());
}
}
/*
* map的key 下划线 转 驼峰格式 , 不利用bean 你是怎么转的?
*只针对key是String类型,其他类型不支持
*
*/
public static Map<String, Object> getMap(Map<String, Object> map) {
Map<String, Object> newMap = new HashMap<>();
if (map != null && map.size() > 0) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (key instanceof String){
Object value = entry.getValue();
if (key.indexOf("_") > 0) {
String[] split = key.split("_");
if (split != null && split.length > 0) {
String vv = "";
for (int i = 0; i < split.length; i++) {
String s = split[i];
if (i == 0) {
if (s.length() > 0) {
vv = s;
}
} else {
if (s.length() > 0) {
char[] chars = s.toCharArray();
if (chars != null && chars.length > 0) {
String replace = s.replace(chars[0], String.valueOf(chars[0]).toUpperCase().charAt(0));
vv += replace;
}
}
}
}
newMap.put(vv, value);
}
}
}
}
}
return newMap;
}
}
|