当前位置:网站首页>工具类:对象转map 驼峰转下划线 下划线转驼峰
工具类:对象转map 驼峰转下划线 下划线转驼峰
2022-07-07 02:40:00 【溜达的大象】
package com.esint.gaplatform.sjxxdataxjclgs.util;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Obj2map {
public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
Map<String, Object> map = new HashMap<String, Object>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
String field_name = humpToLine(fieldName);
Object value = field.get(obj);
map.put(field_name, value);
}
return map;
}
private static Pattern linePattern = Pattern.compile("_(\\w)");
private static Pattern humpPattern = Pattern.compile("[A-Z]");
/** * 驼峰转下划线,最后转为大写 * @param str * @return */
public static String humpToLine(String str) {
Matcher matcher = humpPattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString().toUpperCase();
}
/** * 下划线转驼峰,正常输出 * @param str * @return */
public static String lineToHump(String str) {
Matcher matcher = linePattern.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
}
matcher.appendTail(sb);
return sb.toString();
}
}
边栏推荐
- Learning notes | data Xiaobai uses dataease to make a large data screen
- 2022 Android interview essential knowledge points, a comprehensive summary
- 剑指offer-高质量的代码
- Prompt for channel security on the super-v / device defender side when installing vmmare
- Postgresql源码(59)分析事务ID分配、溢出判断方法
- Leite smart home longhaiqi: from professional dimming to full house intelligence, 20 years of focus on professional achievements
- Abnova 膜蛋白脂蛋白体技术及类别展示
- [start from scratch] detailed process of deploying yolov5 in win10 system (CPU, no GPU)
- Programmers' daily | daily anecdotes
- Etcd database source code analysis -- starting from the start function of raftnode
猜你喜欢
Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
企業如何進行數據治理?分享數據治理4個方面的經驗總結
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
ESXI挂载移动(机械)硬盘详细教程
Go straight to the 2022ecdc fluorite cloud Developer Conference: work with thousands of industries to accelerate intelligent upgrading
企业如何进行数据治理?分享数据治理4个方面的经验总结
线性代数(一)
基于JS的迷宫小游戏
Several index utilization of joint index ABC
The difference between string constants and string objects when allocating memory
随机推荐
Performance comparison between Ceres solver and g2o
一条慢SQL拖死整个系统
Overview of FlexRay communication protocol
How can I check the DOI number of a foreign document?
Leetcode T1165: 日志分析
ip地址那点事
Haqi projection Black Horse posture, avec seulement six mois de forte pénétration du marché des projecteurs de 1000 yuans!
leetcode 509. Fibonacci Number(斐波那契数字)
Postgresql源码(59)分析事务ID分配、溢出判断方法
SVN version management in use replacement release and connection reset
AddressSanitizer 技术初体验
ANR 原理及实践
Programmers' daily | daily anecdotes
偏执的非合格公司
SolidWorks的GB库(钢型材库,包括铝型材、铝管等结构)安装及使用教程(生成铝型材为例)
Basic DOS commands
JVM in-depth
MySQL(十)
FPGA课程:JESD204B的应用场景(干货分享)
Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données