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<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; } /* * 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<String, Object> 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<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(), 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<String, String> 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<Class> clazzs = new ArrayList<Class>(); while(ret){ clazzs.add(oo); oo = oo.getSuperclass(); if(oo == null || oo == Object.class)break; } for(int i=0;i<clazzs.size();i++){ Field[] fields = clazzs.get(i).getDeclaredFields(); for (Field field : fields) { int mod = field.getModifiers(); if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } //由字符串转换回对象对应的类型if (field != null) {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; } //获取关联的所有类,本类以及所有父类 boolean ret = true; Class oo = obj.getClass(); List<Class> clazzs = new ArrayList<Class>(); while(ret){ clazzs.add(oo); oo = oo.getSuperclass(); if(oo == null || oo == Object.class)break; } Map<String, Object> map = new HashMap<String, Object>(); for(int i=0;i<clazzs.size();i++){ Field[] declaredFields = clazzs.get(i).getDeclaredFields(); for (Field field : declaredFields) { int mod = field.getModifiers(); //过滤 static 和 final 类型 if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){ continue; } field.setAccessible(true); map.put(field.getName(), field.get(obj)); } } return map; } public static void main(String[] args) throws Exception {System.out.println(2222);PersonBean pb = new PersonBean("name", 15, 12345678l, true, "10086", "北京市");}}