ตัวอย่างมีดังนี้:
แพ็คเกจ com.ljq.util; นำเข้า java.beans.beaninfo; นำเข้า java.beans.introspector; นำเข้า java.beans.propertydescriptor; นำเข้า java.lang.reflect.method; นำเข้า java.util.arraylist; java.util.map;/** * คลาสเครื่องมือแผนที่ * * @author jqlin */คลาสสาธารณะ maputils {/** * รับค่าแอตทริบิวต์จากคอลเลกชันแผนที่ * * @param <e> * @param map * แผนที่คอลเลกชัน * @param key * คีย์คู่ * @param defaultValue * @return * @author ji "RawTypes"}) สาธารณะสุดท้ายคงที่ <e> e get (แผนที่แผนที่, คีย์วัตถุ, e defaultValue) {object o = map.get (คีย์); ถ้า (o == null) ส่งคืนค่าเริ่มต้น; return (e) o; } / *** วัตถุคอลเลกชันแผนที่ถูกแปลงเป็นวัตถุคอลเลกชัน javabean** @param javabean javabean อินสแตนซ์วัตถุ* @param maplist map dataSet วัตถุ DataSet* @return* @author jqlin* / @suppresswarnings ({"{" {"{" {"{rewtypes"} if (maplist == null || maplist.isempty ()) {return null; } รายการ <t> objectList = new ArrayList <T> (); t object = null; สำหรับ (แผนที่แผนที่: maplist) {ถ้า (แผนที่! = null) {object = map2java (javabean, แผนที่); ObjectList.add (วัตถุ); }} ส่งคืน ObjectList; } / *** วัตถุแผนที่ถูกแปลงเป็นวัตถุ javabean** @param javabean javabean อินสแตนซ์วัตถุ* @param แผนที่วัตถุวัตถุ* @return* @author jqlin* / @suppresswarnings ({"rawtypes", "ไม่ผ่านการตรวจสอบ" Javabean Property beaninfo beaninfo = introspector.getBeanInfo (javabean.getClass ()); // สร้างวัตถุวัตถุ javabean obj = javabean.getClass (). newInstance (); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); if (propertyDescriptors! = null && propertyDescriptors.length> 0) {สตริงคุณสมบัติ name = null; // javabean ชื่อคุณสมบัติ Object PropertyValue = null; // ค่าคุณสมบัติ javabean สำหรับ (propertyDescriptor pd: propertyDescriptors) {propertyName = pd.getName (); if (map.containskey (propertyName)) {propertyValue = map.get (propertyName); pd.getWriteMethod (). revoke (obj, วัตถุใหม่ [] {propertyValue}); }} return (t) obj; }} catch (exception e) {e.printstacktrace (); } return null; } / ** * วัตถุ javabean ถูกแปลงเป็นวัตถุแผนที่ * * @param javabean * @return * @author jqlin * / @suppresswarnings ({"rawtypes", "unchecked"}) แผนที่สาธารณะ java2map ลอง {// รับ javabean property beaninfo beaninfo = introspector.getBeanInfo (javabean.getClass ()); PropertyDescriptor [] PropertyDescriptors = beanInfo.getPropertyDescriptors (); if (propertyDescriptors! = null && propertyDescriptors.length> 0) {สตริงคุณสมบัติ name = null; // javabean ชื่อคุณสมบัติ Object PropertyValue = null; // ค่าคุณสมบัติ javabean สำหรับ (propertyDescriptor pd: propertyDescriptors) {propertyName = pd.getName (); if (! propertyName.equals ("class")) {method readMethod = pd.getReadMethod (); PropertyValue = readMethod.invoke (Javabean, วัตถุใหม่ [0]); map.put (PropertyName, PropertyValue); }}}} catch (Exception E) {E.printStackTrace (); } กลับแผนที่; -ข้างต้นเป็นเนื้อหาเต็มรูปแบบของวิธีการแปลง Javabean และ Map Map ที่นำมาให้คุณโดยบรรณาธิการ ฉันหวังว่าทุกคนจะสนับสนุน wulin.com เพิ่มเติม ~