import java.io.Serializable; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author * 2017-09-25 */ public class MapConvertUtil implements Serializable{ /** * 序列化 */ private static final long serialVersionUID = -1581756576497606672L; private static final Logger LOGGER = LoggerFactory.getLogger(MapConvertUtil.class); public static Double getDoubleValue(Map<String, Object> map, String key) { BigDecimal value = getMapValue(map, key, BigDecimal.class); return value == null ? 0d : value.doubleValue(); } public static Float getFloatValue(Map<String, Object> map, String key) { BigDecimal value = getMapValue(map, key, BigDecimal.class); return value == null ? 0f : value.floatValue(); } public static Date getDateValue(Map<String, Object> map, String key) { Object value = map.get(key); return value == null ? new Date() : new Date(value.toString()); } public static Integer getIntegerValue(Map<String, Object> map, String key) { Object value = map.get(key); if(value != null) { try { return Integer.valueOf(value.toString()); } catch (Exception e) { LOGGER.error("exception message:", e); return 0; } } return 0; } public static Long getLongValue(Map<String, Object> map, String key) { Object value = map.get(key); if(value != null) { try { return Long.valueOf(value.toString()); } catch (Exception e) { LOGGER.error("exception message:", e); return 0l; } } return 0l; } public static String getStringValue(Map<String, Object> map, String key) { String value = getMapValue(map, key, String.class); return value == null ? "" : value; } public static <T> T getMapValue(Map<String, Object> map, String key, Class<T> clazz) { Object value = map.get(key); if (value == null) { return null; } return clazz.cast(value); } public static Map<String, Object> PO2Map(Object o){ Map<String, Object> map = new HashMap<String, Object>(); Field[] fields = null; try { //子类属性 fields = o.getClass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String proName = field.getName(); Object proValue = field.get(o); map.put(proName, proValue); } //父类属性 fields = o.getClass().getSuperclass().getDeclaredFields(); for (Field field : fields) { field.setAccessible(true); String proName = field.getName(); Object proValue = field.get(o); map.put(proName, proValue); } } catch (Exception e) { LOGGER.error("exception message:", e); } return map; } public static Object map2PO(Map<String,Object> map,Object o){ try { if (!map.isEmpty()) { for (Map.Entry<String,Object> m : map.entrySet()) { Object v = ""; if (!m.getKey().isEmpty()) { v = m.getValue(); } Field[] fields = null; //子类属性 fields = o.getClass().getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } if (field.getName().equals(m.getKey())) { field.setAccessible(true); field.set(o, v); } } //父类属性 fields = o.getClass().getSuperclass().getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } if (field.getName().equals(m.getKey())) { field.setAccessible(true); field.set(o, v); } } } } }catch (Exception e) { LOGGER.error("exception message:", e); } return o; } public static Map<String,Object> getMsgResultMap(final String msg) { return new HashMap<String, Object>(){/** */ private static final long serialVersionUID = 3883115499389307532L; { put("code", "0"); put("msg", msg); }}; } public static Map<String,Object> getDataResultMap(final Object data) { return new HashMap<String, Object>(){/** */ private static final long serialVersionUID = 2024109428179733317L; { put("code", "1"); put("msg", "success"); put("data", data); }}; } }
相关推荐
将java实体类转换为map mybatis里用map更方便
java一键xml转map,一键map转xml工具类,代码已封装好,一个方法即可将两种格式文本互转,注意是互转,网上很多只有xml转map,并不支持map转xml
使用xml转json,可以使用这个工具类,先转成map,之后使用自己的json工具类即可。
主要为大家详细介绍了Java常用工具类,包括UUID工具类、Map工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java代码-使用java解决bean&map工具类的源代码 ——学习参考资料:仅用于个人学习使用!
Bean与MAP间互转换的工具类。 1.将一个 Map 对象转化为一个 JavaBean,可指定BEAN的类型; 2.将一个 JavaBean 对象转化为一个 HashMap;
下面小编就为大家带来一篇List转换成Map工具类的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://871421448.iteye.com/blog/2148015
对象与map互相转换工具类
用于 解决在批量数据传输过程中对数据要求转为ListMap格式
字符编码工具类-数据类型转换-日期工具类-Escape中文转码工具类-fastjson工具类-文件工具类-Http工具类-http请求工具类-用于模拟HTTP请求中GET/POST方式 -图片处理工具类-Ip工具类-mail工具类-Map工具类-MD5编码工具...
NULL 博文链接:https://rensanning.iteye.com/blog/1547845
这是一个工具类用于判断map中的键值对是否为空!普通判断若遇上空会报空指针异常!
Utility class to make GoogleMap with custom theme. Create your own theme like this GoogleMapStyler googleMapStyler = new GoogleMapStyler.Builder(MainActivity.this) .setMainGeometryColorRes(R.color....
史上最全的Java工具类,包括http访问,发送短信验证码,拼音转孩子,android图片压缩,...日期生成,相对日期生成,字符串工具类,android sharepreferences工具类,android Toast工具类, android sqlite工具类等等。
文件工具类,Http请求工具类,图片处理工具类。Ip工具类。mail工具类,Map工具类,MD5编码工具类,数字工具类,随机数工具类,反射工具类,字符串处理工具类,URL工具类,XML工具类,常用的数据验证工具类
生活中经常用到map数据与实体类的转换,今天就写了一篇博客,顺便把文中涉及到的代码打包出来。以供参考
c#版本ServiceStack.Redis操作工具类
NULL 博文链接:https://wuniu2010.iteye.com/blog/1771934
Map工具类,可用于Map常用操作,如: isEmpty(Map, V> sourceMap) 判断map是否为空或长度为0 parseKeyAndValueToMap(String source, String keyAndValueSeparator, String keyAndValuePairSeparator, boolean ...