当前位置:网站首页>工具类:对象转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();
}
}
边栏推荐
- [start from scratch] detailed process of deploying yolov5 in win10 system (CPU, no GPU)
- 毕业设计游戏商城
- Stack and queue-p78-8 [2011 unified examination true question]
- Stack and queue-p79-10 [2014 unified examination real question]
- 分布式id解决方案
- Leetcode T1165: 日志分析
- Common problems of caching in high concurrency scenarios
- C language (structure) defines a user structure with the following fields:
- Stack and queue-p79-9
- 华为机试题素数伴侣
猜你喜欢

Stack and queue-p79-9

品牌电商如何逆势增长?在这里预见未来!

2022 Android interview essential knowledge points, a comprehensive summary

MySQL卸载文档-Windows版

途家、木鸟、美团……民宿暑期战事将起

Handling hardfault in RT thread

How to use wechat cloud hosting or cloud functions for cloud development of unapp development applet

剑指offer-高质量的代码

Ant manor safety helmet 7.8 ant manor answer

Cloudcompare point pair selection
随机推荐
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
数据资产管理与数据安全国内外最新趋势
怎样查找某个外文期刊的文献?
Jmeter 5.5版本发布说明
华为机试题素数伴侣
Several index utilization of joint index ABC
MySql用户权限
POI导出Excel:设置字体、颜色、行高自适应、列宽自适应、锁住单元格、合并单元格...
C language interview to write a function to find the first occurrence of substring m in string n.
品牌电商如何逆势增长?在这里预见未来!
Pinduoduo lost the lawsuit: "bargain for free" infringed the right to know but did not constitute fraud, and was sentenced to pay 400 yuan
JWT 认证
A program lets you understand what static inner classes, local inner classes, and anonymous inner classes are
Redis (II) - redis General Command
C language (structure) defines a user structure with the following fields:
软件测试到了35岁,真的就干不动了吗?
Abnova 免疫组化服务解决方案
拼多多败诉:“砍价免费拿”侵犯知情权但不构成欺诈,被判赔400元
Tkinter window selects PCD file and displays point cloud (open3d)
隐马尔科夫模型(HMM)学习笔记