/**
* 使用org.apache.commons.beanutils进行转换
*/
class A { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null; Object obj = beanClass.newInstance(); org.apache.commons.beanutils.BeanUtils.populate(obj, map); return obj;
} public static Map<?, ?> objectToMap(Object obj) {
if(obj == null)
return null; return new org.apache.commons.beanutils.BeanMap(obj);
} } /**
* 使用Introspector进行转换
*/
class B { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null; Object obj = beanClass.newInstance(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
setter.invoke(obj, map.get(property.getName()));
}
} return obj;
} public static Map<String, Object> objectToMap(Object obj) throws Exception {
if(obj == null)
return null; Map<String, Object> map = new HashMap<String, Object>(); BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter!=null ? getter.invoke(obj) : null;
map.put(key, value);
} return map;
} } /**
* 使用reflect进行转换
*/
class C { public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
if (map == null)
return null; Object obj = beanClass.newInstance(); Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
int mod = field.getModifiers();
if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
continue;
} field.setAccessible(true);
field.set(obj, map.get(field.getName()));
} return obj;
} public static Map<String, Object> objectToMap(Object obj) throws Exception {
if(obj == null){
return null;
} Map<String, Object> map = new HashMap<String, Object>(); Field[] declaredFields = obj.getClass().getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
map.put(field.getName(), field.get(obj));
} return map;
}
}

java对象与map对象相互转换的更多相关文章

  1. java 如何遍历Map对象

    内容介绍 在java中遍历Map对象的方法. Map对象 Map<String,Object> map = new HashMap<>(); map.put("xia ...

  2. 转换复杂的JSON对象为 Map对象

    最近项目需要跟客户对接一个webservice接口,客户那传json串过来,属于比较复杂的json串,这里跟大家分享下我项目中所用的解析方法: 该方法需要以下jar package com.test; ...

  3. Java中遍历Map对象的方法

    方法一: 在for-each循环中使用entries来遍历 这是最常见的遍历方式,在需要获取key和value时使用. Map<Integer, Integer> map = new Ha ...

  4. java中遍历map对象的多种方法

    在Java中如何遍历Map对象   How to Iterate Over a Map in Java 在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有ma ...

  5. Java中遍历Map对象的4种方法

    java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHashMap, Hashtable, 等等). HashMap<Inte ...

  6. Java对象与Map间相互转换

    将Java对象转为一个Map,以及将map转为对应Java对象,代码如下: public class BeanMapUtil { private static ConcurrentHashMap< ...

  7. Python中的常用内置对象之map对象

    如果你了解云计算的最重要的计算框架Mapreduce,你就对Python提供的map和reduce对象有很好的理解,在大数据面前,单机计算愈加力不从心,分布式计算也就是后来的云计算的框架担当大任,它提 ...

  8. JavaBean对象与Map对象互相转化

    /** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObject(Map<String ...

  9. JavaScript 对象Array,Map,Set使用

    for(int i = 0 :i < 3 ;i++ ){ //[重点说三遍] 在说明每个对象的用法之前,首先说明 JavaScript 对象的使用一定要注意浏览器的兼容性问题!尤其是IE的版本! ...

随机推荐

  1. Struts2返回json数据xml中配置

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/ ...

  2. mysql的密码忘记了怎么办

    我们的大脑不是电脑,有些东西难免会忘,但是会了这个再也不担心宝宝忘记密码了 (1)点击开始/控制面板/服务/mysql-->右击mysql看属性,里面有mysql的安装地址,然后找到安装地址进行 ...

  3. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  4. fatal error C1859 意外的预编译头错误,只需重新运行编译器(转)

    微软的建议 要解决此问题,请使用下列方法之一. http://support.microsoft.com/kb/976656/zh-cn 方法 1 禁用/analyze编译器选项,则它会被启用. 方法 ...

  5. cached-query 将缓存和查询数据库高速连接起来的轻类库

    介绍 我们经常有这种需求:当我们把memcached增加到项目后我还还要写一个 cacheUtils 或者 cacheManager 之类的类来操作memcached. 而且一般的操作不外乎是这种操作 ...

  6. Linux命令Find实例

    转自: http://www.tecmint.com/35-practical-examples-of-linux-find-command/ 35 Practical Examples of Lin ...

  7. ZegGraph属性含义

    一.主要内容概念 属性名称 属性值.作用 MasterPane 一个类对象管理多个GraphPane来源于PaneBase.使用MasterPane类都是可选的,GraphPane类可以直接用于一个单 ...

  8. openHEVC 编译 for VS2017+Win10 x64

    编译暂未成功,有空再次更新 前期准备: yasm下载:http://yasm.tortall.net/Download.html http://www.tortall.net/projects/yas ...

  9. [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith

    Previously we have seen how to do Unit testing with Mockito; import org.junit.Test; import static or ...

  10. MSSQL存储过程

    存储过程的种类:       1.系统存储过程.        以sp_开头      2.扩展存储过程.        以xp_开头      3.用户定义存储过程. --重新编译存储过程 exec ...