驼峰与下划线互转工具类

时间:2025-02-16 07:14:32

 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 = ("([A-Za-z\\d]+)(_)?");
    Matcher matcher = (line);
    while (()) {
        String word = ();
        (smallCamel && () == 0 ? ((0))
                : ((0)));
        int index = ('_');
        if (index > 0) {
            ((1, index).toLowerCase());
        } else {
            ((1).toLowerCase());
        }
    }
    return ();
}

测试用例:

String line1 = "user_id";
String camel1 = underline2Camel(line1, false);
(camel1);//UserId


String line2 = "order_no";
String camel2 = underline2Camel(line2, true);
(camel2);//orderNo

String line3 = "_first_name_";
String camel3 = underline2Camel(line3, true);
(camel3);//FirstName

String line4 = "PHONE_NUMBER";
String camel4 = underline2Camel(line4, false);
(camel4);//PhoneNumber

String line5 = "product123";
String camel5 = underline2Camel(line5, false);
(camel5);//Product123

String line6 = "";
String camel6 = underline2Camel(line6, true);
(camel6);//

2.驼峰法转下划线

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

测试用例:

转小写:

String underline = camel2Underline("asdAdsDsd");
(underline);//asd_ads_dsd

转大写:

String underline = camel2Underline("asdAdsDsd");
(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 : ()) {
        String camel = underline2Camel(key, true);
        //存在 " " 转换为""
        if (" ".equals((key))) {
            (camel, "");
        } else {
            (camel, (key));
        }
    }
    return newMap;
}

测试用例:

输入:

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

输出:

Map<String, Object> newMap = getUnderline2CamelMap(map); (newMap);

预期输出:

{userName=John Doe, emailAddress=john@}

以下方法都是复用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 : ()) {
        String camel = camel2Underline(key);
        (camel, (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 : ()) {
            String camel = underline2Camel(key, true);
            (camel, (key));
        }
        (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 : ()) {
            String camel = camel2Underline(key);
            (camel, (key));
        }
        (newMap);
    }

    return returnList;
}


/**
 * 下划线转驼峰法List
 *
 * @param list 源字符串
 * @return 转换后的List套String
 */
public static List<String> getList(List<String> list) {
    List<String> returnList = new ArrayList<>();
    ((0));
    for (String key : list) {
        String camel = underline2Camel(key, true);
        (camel);
    }
    return returnList;
}