Examples are as follows:
package com.ljq.util;import java.beans.BeanInfo;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Map tool class* * @author jqlin */public class MapUtils { /** * Get attribute value from map collection* * @param <E> * @param map * map collection* @param key * key pair* @param defaultValue * Default value* @return * @author jiqinlin */ @SuppressWarnings({ "unchecked", "rawtypes" }) public final static <E> E get(Map map, Object key, E defaultValue) { Object o = map.get(key); if (o == null) return defaultValue; return (E) o; } /** * Map collection object is converted into JavaBean collection object* * @param javaBean JavaBean instance object* @param mapList Map dataset object* @return * @author jqlin */ @SuppressWarnings({ "rawtypes" }) public static <T> List<T> map2Java(T javaBean, List<Map> mapList) { if(mapList == null || mapList.isEmpty()){ return null; } List<T> objectList = new ArrayList<T>(); T object = null; for(Map map : mapList){ if(map != null){ object = map2Java(javaBean, map); objectList.add(object); } } return objectList; } /** * Map object is converted into JavaBean object* * @param javaBean JavaBean instance object* @param map Map object* @return * @author jqlin */ @SuppressWarnings({ "rawtypes","unchecked", "hiding" }) public static <T> T map2Java(T javaBean, Map map) { try { // Get the javaBean property BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass()); // Create JavaBean object Object obj = javaBean.getClass().newInstance(); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if (propertyDescriptors != null && propertyDescriptors.length > 0) { String propertyName = null; // javaBean property name Object propertyValue = null; // javaBean property value for (PropertyDescriptor pd : propertyDescriptors) { propertyName = pd.getName(); if (map.containsKey(propertyName)) { propertyValue = map.get(propertyName); pd.getWriteMethod().invoke(obj, new Object[] { propertyValue }); } } return (T) obj; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * JavaBean object is converted into Map object* * @param javaBean * @return * @author jqlin */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Map java2Map(Object javaBean) { Map map = new HashMap(); try { // Get the javaBean property BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if (propertyDescriptors != null && propertyDescriptors.length > 0) { String propertyName = null; // javaBean property name Object propertyValue = null; // javaBean property value for (PropertyDescriptor pd : propertyDescriptors) { propertyName = pd.getName(); if (!propertyName.equals("class")) { Method readMethod = pd.getReadMethod(); propertyValue = readMethod.invoke(javaBean, new Object[0]); map.put(propertyName, propertyValue); } } } } catch (Exception e) { e.printStackTrace(); } return map; } }The above is the full content of the JavaBean and Map conversion package method brought to you by the editor. I hope everyone will support Wulin.com more~