Map把“_”形式的key转化为驼峰形式

常见与数据库获取数据,key为字段名

package com.neusoft.modules.generator.handler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;

/**
 * map工具类
 *
 * @author 
 * @date 2021/8/31 12:57
 */
public class MapUtil {

    /**
     * 把map的key转换成驼峰命名
     *
     * @param map
     * @return map
     */
    public static Map toReplaceKeyLow(Map map) {
    	if(null == map)
    	return null;
        Map reMap = new HashMap<>();
        if (reMap != null) {
            Iterator var2 = map.entrySet().iterator();

            while (var2.hasNext()) {
                Entry entry = (Entry) var2.next();
                String value= String.valueOf(map.get(entry.getKey()));
                if(value.indexOf("0E-")!=-1)
                {
                	value = "0";
                }
                if(value.equals("null"))
                {
                	value = "";
                }
                //将小数后面的“0”去掉,保留2位小数
                if(validateNumber(value))
                {
//                	System.out.println("value = " +value);
                	DecimalFormat decimalFormat = new DecimalFormat("###.##");
//                	String str = value.substring(0,5);
//                	if (str.equals("0.005")) {
//                	    value = "0.01";
//                    } else {
                        value = decimalFormat.format(Float.valueOf(value).floatValue());
//                    }
                }
                reMap.put(underlineToCamel((String) entry.getKey()),value);
            }

            map.clear();
        }

        return reMap;
    }

    public static final char UNDERLINE = '_';


    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(Character.toLowerCase(param.charAt(i)));
            }
        }
        return sb.toString();
    }
    
    public static  List deepCopy(List src)
            throws IOException, ClassNotFoundException
        {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            return (List)in.readObject();
        }
    //判断该数据是否为小数形式
    private static boolean validateNumber(String str) {
    	 
        if(StringUtils.isBlank(str)||str.indexOf(".")==-1) {
     
          return false;
     
        }
     
        // 说明一下的是该正则只能识别4位小数;如果不限制小数位数的话,写成[+-]?[0-9]+(\\.[0-9]+)?就可以了
     
        return str.matches("[+-]?[0-9]+(\\.[0-9]+)?");  
    }
     

}

你可能感兴趣的:(java,开发语言)