当前位置:网站首页>Tool class: object to map hump to underline underline hump
Tool class: object to map hump to underline underline hump
2022-07-07 06:57:00 【Wandering elephant】
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]");
/** * Hump underline , Finally capitalize * @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();
}
/** * Underline to hump , Normal output * @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();
}
}
边栏推荐
猜你喜欢
数据资产管理与数据安全国内外最新趋势
二十岁的我4面拿到字节跳动offer,至今不敢相信
Mysql---- import and export & View & Index & execution plan
From zero to one, I will teach you to build the "clip search by text" search service (2): 5 minutes to realize the prototype
Can 7-day zero foundation prove HCIA? Huawei certification system learning path sharing
联合索引ABC的几种索引利用情况
SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)
健身房如何提高竞争力?
[GNN] graphic gnn:a gender Introduction (including video)
SVN version management in use replacement release and connection reset
随机推荐
POI export to excel: set font, color, row height adaptation, column width adaptation, lock cells, merge cells
ESXI挂载移动(机械)硬盘详细教程
sqlserver多线程查询问题
MYSQL----导入导出&视图&索引&执行计划
.net core 访问不常见的静态文件类型(MIME 类型)
Postgresql中procedure支持事务语法(实例&分析)
Matlab tips (29) polynomial fitting plotfit
SVN version management in use replacement release and connection reset
品牌·咨询标准化
How can gyms improve their competitiveness?
js装饰器@decorator学习笔记
Bus消息总线
Several index utilization of joint index ABC
使用net core优势/为什么使用
RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`问题解决
Abnova循环肿瘤DNA丨全血分离,基因组DNA萃取分析
Algorithm --- bit count (kotlin)
Google Chrome browser released patch 103.0.5060.114 to fix the 0-day vulnerability
偏执的非合格公司
[solution] final app status- undefined, exitcode- 16