当前位置:网站首页>工具类:对象转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();
}
}
边栏推荐
- 2022/07/04学习记录
- Postgresql源码(59)分析事务ID分配、溢出判断方法
- Kotlin之 Databinding 异常
- Crudini profile editing tool
- MySQL的安装
- 常用函数detect_image/predict
- Prompt for channel security on the super-v / device defender side when installing vmmare
- C interview 24 (pointer) define a double array with 20 elements a
- Unity C# 函数笔记
- Navicat导入15G数据报错 【2013 - Lost connection to MySQL server during query】 【1153:Got a packet bigger】
猜你喜欢
2022年全国所有A级景区数据(13604条)
屏幕程序用串口无法调试情况
使用TCP/IP四层模型进行网络传输的基本流程
string(讲解)
JWT 认证
Stack and queue-p79-10 [2014 unified examination real question]
[GNN] graphic gnn:a gender Introduction (including video)
MySQL installation
Answer to the first stage of the assignment of "information security management and evaluation" of the higher vocational group of the 2018 Jiangsu Vocational College skills competition
Redhat5 installing vmware tools under virtual machine
随机推荐
程序员的日常 | 每日趣闻
华为机试题素数伴侣
【NOI模拟赛】区域划分(结论,构造)
Postgresql中procedure支持事务语法(实例&分析)
什么情况下考虑分库分表
C language interview to write a function to find the first occurrence of substring m in string n.
Doctoral application | Professor Hong Liang, Academy of natural sciences, Shanghai Jiaotong University, enrolls doctoral students in deep learning
ViewModelProvider.of 过时方法解决
软件测试到了35岁,真的就干不动了吗?
地质学类比较有名的外文期刊有哪些?
Install mongodb database
sqlserver多线程查询问题
Force deduction 62 different paths (the number of all paths from the upper left to the lower right of the matrix) (dynamic planning)
怎样查找某个外文期刊的文献?
Leetcode T1165: 日志分析
LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates
数据资产管理与数据安全国内外最新趋势
What are the classic database questions in the interview?
使用net core优势/为什么使用
C language interview to write a function to find the first public string in two strings