Java的Map与Object互转有三种方法:

1.使用 org.apache.commons.beanutils 进行转换;

使用简单方便,但需要引入第三方包。


2.使用 Introspector 进行转换;

getPropertyDescriptors()根据方法来得到属性信息,所有符合javabean的get、set方法都会被获取到,需要自己过滤不是属性的方法;

 

3.使用 reflect 进行转换;

getDeclaredFields()会获取所有属性,需要过滤不需要的类型如 static final等,不会获取父类的属性,需要自己编码实现


通过简单性能测试发现:

org.apache.commons.beanutils性能最差,使用 reflect 性能最好。

下面是代码示例:

/** 
 * 使用org.apache.commons.beanutils进行转换 
 */ 
class ApacheBeanUtils {  
       
	/*
	 * Populate the JavaBeans properties of the specified bean, based on
     * the specified name/value pairs.
	 */
    public Object mapToObject(Map map, Class beanClass) throws Exception {    
        if (map == null)  
            return null;  
   
        Object obj = beanClass.newInstance();  
   
        org.apache.commons.beanutils.BeanUtils.populate(obj, map);  
   
        return obj;  
    }    
       
    /*
     *  An implementation of Map for JavaBeans which uses introspection to
     * get and put properties in the bean.
     */
    public Map objectToMap(Object obj)  throws Exception {  
        if(obj == null)  
            return null;   
   
        return new org.apache.commons.beanutils.BeanMap(obj);  
    }    
       
}
/** 
 * 使用Introspector进行转换 
 * getPropertyDescriptors()根据方法来得到属性信息,所有符合javabean的get、set方法都会被获取到
 * 需要自己过滤不是属性的方法
 * 耗时与reflect相比:100:1
 */ 
public class IntrospectorBeanUtils{

	public static Object mapToObject(Map map, Class beanClass) throws Exception {    
        if (map == null || map.size()<=0)   
            return null;    
   
        Object obj = beanClass.newInstance();  
   
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);    
        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 objectToMap(Object obj) throws Exception {    
        if(obj == null)  
            return null;      
   
        Map map = new HashMap();   
   
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);    
        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进行转换 
 * getDeclaredFields()会获取所有属性,需要过滤不需要的类型如 static final
 * 不会获取父类的属性,需要自己编码实现
 * 耗时与Introspector相比:1:100
 */ 
public class ReflectBeanUtils{

	public static Object mapToObject(Map map, Class beanClass) throws Exception {    
        if (map == null || map.size()<=0)   
            return null;    
   
        Object obj = beanClass.newInstance();  
        //获取关联的所有类,本类以及所有父类
        boolean ret = true;
        Class oo = obj.getClass();
        List clazzs = new ArrayList();
        while(ret){
        	clazzs.add(oo);
        	oo = oo.getSuperclass();
        	if(oo == null || oo == Object.class)break;
        }
        
       for(int i=0;i objectToMap(Object obj) throws Exception {    
        if(obj == null){    
            return null;    
        }   
        //获取关联的所有类,本类以及所有父类
        boolean ret = true;
        Class oo = obj.getClass();
        List clazzs = new ArrayList();
        while(ret){
        	clazzs.add(oo);
        	oo = oo.getSuperclass();
        	if(oo == null || oo == Object.class)break;
        }
        
        Map map = new HashMap();    
   
       for(int i=0;i