예는 다음과 같습니다.
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. 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 (맵 맵, 객체 키, e defaultValue) {object o = map.get (키); if (o == null) defaultValue를 반환합니다. 반품 (e) o; } / ***지도 수집 객체는 Javabean Collection Object** @param javabean javabean 인스턴스 개체* @param maplist 맵 데이터 세트 객체* @return* @author jqlin* / @suppresswarnings ({ "rawtypes"}) public static <t> 목록 <T> map2Java (t javaBean, list> if (maplist) {maplist> maplist. NULL ||. Maplist.isempty ()) {return null; } list <t> objectList = new ArrayList <t> (); t 객체 = null; for (맵 맵 : maplist) {if (map! = null) {object = map2java (javabean, map); ObjectList.add (Object); }} return objectList; } / *** 맵 객체는 Javabean 객체로 변환됩니다** @param javabean javabean 인스턴스 개체* @param 맵 객체* @return* @author jqlin* / @suppresswarnings ({ "rawtypes", "unchecked", "hiding") public static <t> t map2java (t javabean, 맵 맵). Javabean 속성 BeanInfo BeanInfo = introspector.getBeanInfo (javabean.getClass ()); // javabean 객체 객체 생성 obj = javabean.getClass (). newInstance (); PropertyDescriptor [] propertyDescriptors = beanInfo.getPropertyDescriptors (); if (propertyDescriptors! = null && propertyDescriptors.length> 0) {String PropertyName = null; // javabean 속성 이름 객체 propertyvalue = null; // (PropertyDescriptor PD : PropertyDescriptors)의 Javabean 속성 값 {propertyName = pd.getName (); if (map.containskey (propertyName)) {propertyValue = map.get (propertyName); pd.getWritemEthod (). invoke (obj, new Object [] {propertyValue}); }} return (t) obj; }} catch (예외 e) {e.printstacktrace (); } return null; } / ** * javabean 객체는 맵 객체로 변환됩니다 * * @param javabean * @return * @author jqlin * / @suppresswarnings ({ "rawtypes", "unchecked"}) 공개 정적지도 java2map (객체 javabean) {map map = new hashmap (); {// Javabean 속성 BeanInfo BeanInfo = introspector.getBeanInfo (javabean.getClass ()); PropertyDescriptor [] propertyDescriptors = beanInfo.getPropertyDescriptors (); if (propertyDescriptors! = null && propertyDescriptors.length> 0) {String PropertyName = null; // javabean 속성 이름 객체 propertyvalue = null; // (PropertyDescriptor PD : PropertyDescriptors)의 Javabean 속성 값 {propertyName = pd.getName (); if (! propertyName.equals ( "class")) {메소드 readMethod = pd.getReadMethod (); PropertyValue = readMethod.invoke (Javabean, New Object [0]); map.put (PropertyName, PropertyValue); }}}} catch (예외 e) {e.printstacktrace (); } 리턴 맵; }}위는 편집자가 가져온 Javabean 및 MAP 변환 패키지 방법의 전체 내용입니다. 모두가 wulin.com을 더 지원하기를 바랍니다