實(shí)例如下:
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工具類 * * @author jqlin */public class MapUtils {  /**   * 從map集合中獲取屬性值   *    * @param <E>   * @param map   *      map集合   * @param key   *      鍵對(duì)   * @param defaultValue   *      默認(rèn)值   * @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集合對(duì)象轉(zhuǎn)化成 JavaBean集合對(duì)象   *    * @param javaBean JavaBean實(shí)例對(duì)象   * @param mapList Map數(shù)據(jù)集對(duì)象   * @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對(duì)象轉(zhuǎn)化成 JavaBean對(duì)象   *    * @param javaBean JavaBean實(shí)例對(duì)象   * @param map Map對(duì)象   * @return   * @author jqlin   */  @SuppressWarnings({ "rawtypes","unchecked", "hiding" })  public static <T> T map2Java(T javaBean, Map map) {    try {      // 獲取javaBean屬性      BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());      // 創(chuàng)建 JavaBean 對(duì)象      Object obj = javaBean.getClass().newInstance();      PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();      if (propertyDescriptors != null && propertyDescriptors.length > 0) {        String propertyName = null; // javaBean屬性名        Object propertyValue = null; // javaBean屬性值        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對(duì)象轉(zhuǎn)化成Map對(duì)象   *    * @param javaBean   * @return   * @author jqlin   */  @SuppressWarnings({ "rawtypes", "unchecked" })  public static Map java2Map(Object javaBean) {    Map map = new HashMap();         try {      // 獲取javaBean屬性      BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());      PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();      if (propertyDescriptors != null && propertyDescriptors.length > 0) {        String propertyName = null; // javaBean屬性名        Object propertyValue = null; // javaBean屬性值        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;  } }以上就是小編為大家?guī)淼腏avaBean和Map轉(zhuǎn)換封裝類的方法全部?jī)?nèi)容了,希望大家多多支持武林網(wǎng)~
新聞熱點(diǎn)
疑難解答
圖片精選