开发者

java map的key值转驼峰命名的方法

开发者 https://www.devze.com 2023-12-05 10:21 出处:网络 作者: 寒冷的雨呢
Java map的key值转驼峰 MPJLambdaWrapper<Entity> wrapper = new MPJLambdaWrapper<Entity>();

Java map的key值转驼峰

MPJLambdaWrapper<Entity> wrapper = new MPJLambdaWrapper<Entity>();
baseMapper.selectJoinMaps(wrapper).stream().map(MapUtil::toCamelCaseMap).collect(Collectors.toList());
List<Map<String, Object>> getList(DateAndAreaDto dateAndAreaDto);
return Result.success(scaleoperateMapper.getList(dateAndAreaDto)
                .stream()
                .map(MapUtil::toCamelCaseMap)
             map.stream()
                .map(MapUtil::toCamelCaseMap)
                .collect(Collectors.toList())

Java 把map的key转换成驼峰命名 

需求:将map里的key转换为驼峰命名,并返回

package com.enation.app.javashop.framework.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/**
 * map工具类
 *
 * @author 
 * @date 2021/8/31 12:57
 */
public class MapUtils {
    /**
     * 把map的key转换成驼峰命名
     *
     * @param map
     * @return map
     */
    public static Map<String, Object> toReplaceKeyLow(Map<String, Object> map) {
        Map<String, Object> reMap = new HashMap<>();
        if (reMap != null) {
            Iterator var2 = map.entrySet().iterator();
            while (var2.hasNext()) {
                Entry<String, Object> entry = (Entry) var2.next();
                reMap.put(underlineToCamel((String) entry.getKey()), map.get(entry.getKey()));
            }
            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();
    }
}

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<String, String> toReplaceKeyLow(Map<String,编程客栈 String> map) {
    	if(null == map)
    	return null;
        Map<String, String> reMap = new HashMap<>();
        if (reMap != null) {
            Iterator var2 = map.entrySet().iterator();
            while (var2.hasNext()) {
                Entry<String, String> entry = (Entry) var2.next();
                String value= String.valueOf(map.get(entry.getKey()))android;
                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 (phpstr.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 <T> List<T> deepCopy(List<T> 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<T>)in.readObject();
        }
    //判断该数据是否为小数形式
python    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 map的key值转驼峰的文章就介绍到这了,更多相关java map的key值转驼峰内容请搜索编程客javascript栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号