java实现Object和Map之间的转换3种方式

发布时间:2020-10-17 18:42:04 作者:古越剑箫
来源:脚本之家 阅读:231

利用commons.BeanUtils实现Obj和Map之间转换,这种是最简单,也是最经常用的

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);
  }

利用java reflect完成Obj和Map之间的相互转换

    public Map<String,Object> Obj2Map(Object obj) throws Exception{
      Map<String,Object> map=new HashMap<String, Object>();
      Field[] fields = obj.getClass().getDeclaredFields();
      for(Field field:fields){
        field.setAccessible(true);
        map.put(field.getName(), field.get(obj));
      }
      return map;
    }
    public Object map2Obj(Map<String,Object> map,Class<?> clz) throws Exception{
      Object obj = clz.newInstance();
      Field[] declaredFields = obj.getClass().getDeclaredFields();
      for(Field field:declaredFields){
        int mod = field.getModifiers(); 
        if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){
          continue;
        }
        field.setAccessible(true);
        field.set(obj, map.get(field.getName()));
      }
      return obj;
    }

利用Introspector完成Obj和Map之间的相互转换

public Map<String,Object> obj2Map(Object obj) throws Exception{
      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;
    }
    
    public Object map2Obj(Map<String,Object> map,Class<?> clz) throws Exception{
      if (map == null)  
        return null;  
      Object obj = clz.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;
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. 关于元组,元组和列表之间的转换
  2. 在Java中将List转换为Map的方式有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java map object

上一篇:java算法之二分查找法的实例详解

下一篇:短网址的原理与生成方法(Java实现)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》