当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

大促过后,销量与流量兼具,是否真的高枕无忧?

SolidWorks GB Library (steel profile library, including aluminum profile, aluminum tube and other structures) installation and use tutorial (generating aluminum profile as an example)

BindingException 异常(报错)处理

2018年江苏省职业院校技能大赛高职组“信息安全管理与评估”赛项任务书第二阶段答案

Comment les entreprises gèrent - elles les données? Partager les leçons tirées des quatre aspects de la gouvernance des données

精准时空行程流调系统—基于UWB超高精度定位系统

What books can greatly improve programming ideas and abilities?

Abnova 体外转录 mRNA工作流程和加帽方法介绍

How to share the same storage among multiple kubernetes clusters

毕业设计游戏商城
随机推荐
7天零基础能考证HCIA吗?华为认证系统学习路线分享
JWT certification
SVN version management in use replacement release and connection reset
Navicat importing 15g data reports an error [2013 - lost connection to MySQL server during query] [1153: got a packet bigger]
Several index utilization of joint index ABC
How to share the same storage among multiple kubernetes clusters
LM small programmable controller software (based on CoDeSys) Note 23: conversion of relative coordinates of servo motor operation (stepping motor) to absolute coordinates
Bus消息总线
MySQL user permissions
FPGA课程:JESD204B的应用场景(干货分享)
化工园区危化品企业安全风险智能化管控平台建设四大目标
Config分布式配置中心
Stack and queue-p79-9
Postgresql中procedure支持事务语法(实例&分析)
学术报告系列(六) - Autonomous Driving on the journey to full autonomy
How to install swoole under window
一文带你了解静态路由的特点、目的及配置基本功能示例
[noi simulation] regional division (conclusion, structure)
ip地址那点事
【解决】Final app status- UNDEFINED, exitCode- 16