Map object and JavaBean, list and list Interchange

Source: Internet
Author: User
Tags object object

directly on the code.
The following maps are converted to JavaBean using generics, which are more adaptable.
Converting JavaBean to a Map object has not been optimized.

The third and fourth methods are upgrades to the first two methods.

Key Package References list import com.sun.org.apache.commons.beanutils.BeanUtils;
Import Java.beans.BeanInfo;
Import Java.beans.Introspector;
 Import Java.beans.PropertyDescriptor; /** * Converts a Map object to JavaBean This method has been tested by * @author wyply115 * @param type to be converted * @param map *  Return Object * @version March 20, 2016 11:03:01/public static <T> T Convertmap2bean (map map, Class
        T) throws Exception {if (Map==null | | | map.size () ==0) {return null;  
        } BeanInfo BeanInfo = Introspector.getbeaninfo (T); 
        t bean = (t) t.newinstance ();  
        propertydescriptor[] PropertyDescriptors = Beaninfo.getpropertydescriptors (); for (int i = 0, n = propertydescriptors.length i <n i++) {PropertyDescriptor descriptor = Propertydes  
            Criptors[i]; 
            String propertyname = Descriptor.getname ();
            String upperpropertyname = Propertyname.touppercase (); if (Map.containskey (upPerpropertyname)) {Object value = Map.get (Upperpropertyname);
                This method does not report an error that does not match the parameter type.
Beanutils.copyproperty (Bean, PropertyName, value);
Using this method to the type of int and other types will report parameter type mismatch error, we need to manually determine the type of conversion, more trouble.
Descriptor.getwritemethod (). Invoke (bean, value);
Using this method to the type of time and other types will be reported parameter type mismatch error, we also need to manually determine the type of conversion, more trouble.
            Beanutils.setproperty (Bean, PropertyName, value);  
    } return bean;
     /** * Convert JavaBean object to Map This method does not test * @author wyply115 * @param bean Type * @return Map object to convert  
        * @version March 20, 2016 11:03:01 */public static Map Convertbean2map (Object Bean) throws Exception {  
        Class type = Bean.getclass ();  
        Map Returnmap = new HashMap ();  
        BeanInfo BeanInfo = Introspector.getbeaninfo (type);  
        propertydescriptor[] PropertyDescriptors = beanInfo. Getpropertydescriptors ();  
 for (int i = 0, n = propertydescriptors.length i <n; i++) {           PropertyDescriptor descriptor = propertydescriptors[i];  
            String propertyname = Descriptor.getname ();  
                if (!propertyname.equals ("class")) {Method Readmethod = Descriptor.getreadmethod ();  
                Object result = Readmethod.invoke (bean, new object[0]);  
                if (result!= null) {Returnmap.put (propertyname, result);  
                else {returnmap.put (propertyname, "");  
    }} return returnmap; 
     /** * Converts list<map> objects to list<javabean> This method has passed the test * @author wyply115 * @param type to be converted * @param map * @return Object Object * @version March 20, 2016 11:03:01 * * public static <T> LIST&L T T> Convertlistmap2listbean (list<map<string,object>> listmap, Class T) throws Exception {list<
        t> beanlist = new arraylist<t> (); FoR (int i=0, n=listmap.size (); i<n; i++) {map<string,object> Map = Listmap.get (i);
            T bean = Convertmap2bean (map,t);
        Beanlist.add (Bean);  
    return beanlist; /** * Converts list<javabean> objects to list<map> * @author wyply115 * @param type to be converted * @p Aram Map * @return Object Objects * @version March 20, 2016 11:03:01 * * public static List<map<string,ob Ject>> Convertlistbean2listmap (list<object> beanlist) throws Exception {List<map<string,objec
        t>> maplist = new arraylist<map<string,object>> ();
            for (int i=0, n=beanlist.size (); i<n; i++) {Object bean = beanlist.get (i);
            Map map = Convertbean2map (bean);
        Maplist.add (map);  
    return maplist; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.