当前位置:网站首页>工具类:对象转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();
}
}
边栏推荐
- Pinduoduo lost the lawsuit: "bargain for free" infringed the right to know but did not constitute fraud, and was sentenced to pay 400 yuan
- SVN version management in use replacement release and connection reset
- MYSQL----导入导出&视图&索引&执行计划
- Tkinter window selects PCD file and displays point cloud (open3d)
- FPGA课程:JESD204B的应用场景(干货分享)
- Postgresql中procedure支持事务语法(实例&分析)
- The difference between string constants and string objects when allocating memory
- Install mongodb database
- Ha Qu projection dark horse posture, only half a year to break through the 1000 yuan projector market!
- mobx 知识点集合案例(快速入门)
猜你喜欢
精准时空行程流调系统—基于UWB超高精度定位系统
【从零开始】win10系统部署Yolov5详细过程(CPU,无GPU)
MySQL的安装
Matlab / envi principal component analysis implementation and result analysis
「运维有小邓」符合GDPR的合规要求
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
Cloudcompare point pair selection
How can I check the DOI number of a foreign document?
Abnova循环肿瘤DNA丨全血分离,基因组DNA萃取分析
随机推荐
算法---比特位计数(Kotlin)
ceres-solver和g2o性能比较
Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
Abnova 免疫组化服务解决方案
Abnova 膜蛋白脂蛋白体技术及类别展示
C interview encryption program: input plaintext by keyboard, convert it into ciphertext through encryption program and output it to the screen.
分布式id解决方案
String (explanation)
ViewModelProvider.of 过时方法解决
Overview of FlexRay communication protocol
如何给目标机器人建模并仿真【数学/控制意义】
项目实战 五 拟合直线 获得中线
[opencv] morphological filtering (2): open operation, morphological gradient, top hat, black hat
JVM in-depth
品牌·咨询标准化
MATLAB小技巧(30)非线性拟合 lsqcurefit
Can't you really do it when you are 35 years old?
What are the classic database questions in the interview?
Redis (II) - redis General Command
【NOI模拟赛】区域划分(结论,构造)