الأمثلة على النحو التالي:
package com.ljq.Util ؛ استيراد java.beans.beaninfo ؛ استيراد java.beans.introspector ؛ استيراد java.beans.propertydescriptor ؛ import java.lang.reflect.method ؛ import java.util.arraylist ؛ * فئة أداة الخريطة * * Author jqlin */public class mapuTils {/** * الحصول على قيمة السمة من مجموعة الخريطة * * param <e> * param map * map collection * @param pair * @param defaultvalue * default value * regait statin * @author jiqinlin */ @ @suppresswarnings ( <e> e get (خريطة الخريطة ، مفتاح الكائن ، e defaultValue) {object o = map.get (key) ؛ إذا (o == null) إرجاع defaultValue ؛ العودة (هـ) س ؛ } / *** يتم تحويل كائن مجموعة MAP إلى كائن مجموعة javabean** param javabean javabean كائن مثيل javabean* @param maplist map conject* @return* uuthor jqlin* / suppressWarnings ({"rawtyes"}) ||. } list <T> ObjectList = new ArrayList <T> () ؛ t object = null ؛ لـ (خريطة الخريطة: maplist) {if (map! = null) {object = map2java (javabean ، map) ؛ ObjectList.add (كائن) ؛ }} return ObjectList ؛ } / *** يتم تحويل كائن الخريطة إلى كائن javabean** param javabean javabean مثيل مثيل* كائن خريطة param* reghor* author jqlin* / suppresswarnings ({"rawtypes" ، "unchecked" ، "Hiding"}) javabean property beaninfo beaninfo = interspector.getBeanInfo (javabean.getClass ()) ؛ // إنشاء كائن Javabean Object OBJ = javabean.getClass (). newInstance () ؛ propertyDescriptor [] propertyDescriptors = beaninfo.getPropertyDescriptors () ؛ if (propertyDescriptors! = null && propertyDescriptors.length> 0) {String propertyName = null ؛ // javabean الاسم اسم الكائن propertyValue = null ؛ // قيمة خاصية javabean لـ (propertyDescriptor pd: propertyDescriptors) {propertyName = pd.getName () ؛ if (map.containskey (propertyName)) {propertyValue = map.get (propertyName) ؛ pd.getWriteMethod (). invoke (obj ، كائن جديد [] {propertyValue}) ؛ }} return (t) obj ؛ }} catch (استثناء e) {E.PrintStackTrace () ؛ } إرجاع فارغ ؛ } / ** * يتم تحويل كائن javabean إلى كائن MAP * * param javabean * return * author jqlin * / suppressWarnings ({"rawtypes" ، "uncheced"}) الخريطة الثابتة العامة java2map (كائن javabean) حاول {// الحصول على خاصية javabean beaninfo beaninfo = interspector.getBeanInfo (javabean.getClass ()) ؛ propertyDescriptor [] propertyDescriptors = beaninfo.getPropertyDescriptors () ؛ if (propertyDescriptors! = null && propertyDescriptors.length> 0) {String propertyName = null ؛ // javabean الاسم اسم الكائن 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 (استثناء e) {E.PrintStackTrace () ؛ } خريطة الإرجاع ؛ }}ما سبق هو المحتوى الكامل لطريقة حزمة تحويل Javabean و MAP التي قدمها لك المحرر. آمل أن يدعم الجميع wulin.com أكثر ~