驼峰与下划线互转工具类

 1.下划线转驼峰法

 

@param line       要转换的下划线命名字符串。
@param smallCamel 是否使用小驼峰命名方式,即首字母小写。
@return 转换后的字符串
public static String underline2Camel(String line, boolean smallCamel) {
    if (line == null || "".equals(line)) {
        return "";
    }
    StringBuffer sb = new StringBuffer();
    Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
        String word = matcher.group();
        sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0))
                : Character.toUpperCase(word.charAt(0)));
        int index = word.lastIndexOf('_');
        if (index > 0) {
            sb.append(word.substring(1, index).toLowerCase());
        } else {
            sb.append(word.substring(1).toLowerCase());
        }
    }
    return sb.toString();
}

测试用例:

String line1 = "user_id";
String camel1 = underline2Camel(line1, false);
System.out.println(camel1);//UserId


String line2 = "order_no";
String camel2 = underline2Camel(line2, true);
System.out.println(camel2);//orderNo

String line3 = "_first_name_";
String camel3 = underline2Camel(line3, true);
System.out.println(camel3);//FirstName

String line4 = "PHONE_NUMBER";
String camel4 = underline2Camel(line4, false);
System.out.println(camel4);//PhoneNumber

String line5 = "product123";
String camel5 = underline2Camel(line5, false);
System.out.println(camel5);//Product123

String line6 = "";
String camel6 = underline2Camel(line6, true);
System.out.println(camel6);//

2.驼峰法转下划线

@param line 源字符串
@return 转换后的字符串
public static String camel2Underline(String line) {
    if (line == null || "".equals(line)) {
        return "";
    }
    line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
    StringBuffer sb = new StringBuffer();
    Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
    Matcher matcher = pattern.matcher(line);
    while (matcher.find()) {
        String word = matcher.group();
        //根据需要将结果转为大写还是小写,请注意这里!!!
        //sb.append(word.toLowerCase());
        //sb.append(word.toUpperCase());
        sb.append(matcher.end() == line.length() ? "" : "_");
    }
    return sb.toString();
}

测试用例:

转小写:

String underline = camel2Underline("asdAdsDsd");
System.out.println(underline);//asd_ads_dsd

转大写:

String underline = camel2Underline("asdAdsDsd");
System.out.println(underline);//ASD_ADS_DSD

3.下划线转驼峰Map集

public static Map<String, Object> getUnderline2CamelMap(Map<String, Object> map) {
    Map<String, Object> newMap = new HashMap<>();
    for (String key : map.keySet()) {
        String camel = underline2Camel(key, true);
        //存在 " " 转换为""
        if (" ".equals(map.get(key))) {
            newMap.put(camel, "");
        } else {
            newMap.put(camel, map.get(key));
        }
    }
    return newMap;
}

测试用例:

输入:

Map<String, Object> map = new HashMap<>(); map.put("user_name", "John Doe"); map.put("email_address", "john@example.com");

输出:

Map<String, Object> newMap = getUnderline2CamelMap(map); System.out.println(newMap);

预期输出:

{userName=John Doe, emailAddress=john@example.com}

以下方法都是复用underline2Camel()和camel2Underline(),大家自行去测试
/**
 * Map集
 * 驼峰转下划线
 *
 * @param map 源字符串
 * @return 转换后的Map
 */
public static Map<String, Object> getCamel2UnderlineMap(Map<String, Object> map) {
    Map<String, Object> newMap = new HashMap<>();
    for (String key : map.keySet()) {
        String camel = camel2Underline(key);
        newMap.put(camel, map.get(key));
    }
    return newMap;
}


/**
 * 驼峰法转下划线List套Map集
 *
 * @param list 源字符串
 * @return 转换后的List套Map
 */
public static List<Map<String, Object>> getUnderline2CamelList(List<Map<String, Object>> list) {
    Map<String, Object> newMap = new HashMap<>();
    List<Map<String, Object>> returnList = new ArrayList<>();
    for (Map<String, Object> map : list) {
        for (String key : map.keySet()) {
            String camel = underline2Camel(key, true);
            newMap.put(camel, map.get(key));
        }
        returnList.add(newMap);
    }

    return returnList;
}


/**
 * 下划线转驼峰法List套Map集
 *
 * @param list 源字符串
 * @return 转换后的List套Map
 */
public static List<Map<String, Object>> getCamel2UnderlineList(List<Map<String, Object>> list) {
    Map<String, Object> newMap = new HashMap<>();
    List<Map<String, Object>> returnList = new ArrayList<>();
    for (Map<String, Object> map : list) {
        for (String key : map.keySet()) {
            String camel = camel2Underline(key);
            newMap.put(camel, map.get(key));
        }
        returnList.add(newMap);
    }

    return returnList;
}


/**
 * 下划线转驼峰法List
 *
 * @param list 源字符串
 * @return 转换后的List套String
 */
public static List<String> getList(List<String> list) {
    List<String> returnList = new ArrayList<>();
    System.out.println(list.get(0));
    for (String key : list) {
        String camel = underline2Camel(key, true);
        returnList.add(camel);
    }
    return returnList;
}
  • 44
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值