JavaBean Recursive Map implementation

Source: Internet
Author: User

Before looking for such a method, found are not recursive implementation, the list, map inside did not turn, on their own toss A. --yoyo

PublicClassobjecttomap{PublicStaticMap objecttomap (Object obj) {Try{Class type =Obj.getclass (); Map Returnmap =NewHashMap (); BeanInfo BeanInfo =Introspector.getbeaninfo (type); propertydescriptor[] PropertyDescriptors =Beaninfo.getpropertydescriptors ();for (int i = 0; i< propertydescriptors.length; i++) {PropertyDescriptor descriptor =Propertydescriptors[i]; String propertyname =Descriptor.getname ();if (!propertyname.equals ("Class") {Method Readmethod =Descriptor.getreadmethod (); Object result = Readmethod.invoke (obj,New Object[0]);if (result = =Null){Continue; }//Determine if the underlying type string,boolean,byte,short,integer,long,float,double//Determine whether the collection class, Collection,mapif (ResultinstanceofString | | ResultinstanceofBoolean | | ResultinstanceofByte | | ResultinstanceofShort | | ResultinstanceofInteger | | ResultinstanceofLong | | ResultinstanceofFloat | | ResultinstanceofDouble | | ResultinstanceofEnum) {if (Result! =Null{Returnmap.put (propertyname, result);}}Elseif (ResultinstanceofCollection) {collection<?> lstobj = Arraytomap ((collection<?>) result); Returnmap.put (PropertyName, Lstobj); }Elseif (ResultinstanceofMAP) {map<object,object> lstobj = Maptomap ((map<object,object>) result); Returnmap.put (PropertyName, Lstobj); }Else{Map Mapresult =Objecttomap (result); Returnmap.put (PropertyName, Mapresult); } } }ReturnReturnmap; }Catch(Exception e) {ThrowNewRuntimeException (e); } }PrivateStatic Map<object, object> Maptomap (Map<object, object>Orignmap) {map<object,object> Resultmap =New hashmap<object,object>();For (Entry<object, object>Entry:orignMap.entrySet ()) {Object key =Entry.getkey (); Object Resultkey =Null;if (keyinstanceofCollection) {Resultkey =Arraytomap ((Collection) key); }Elseif (keyinstanceofMAP) {Resultkey =Maptomap ((MAP) key); }Else{if (keyinstanceofString | | KeyinstanceofBoolean | | KeyinstanceofByte | | KeyinstanceofShort | | KeyinstanceofInteger | | KeyinstanceofLong | | KeyinstanceofFloat | | KeyinstanceofDouble | | KeyinstanceofEnum) {if (Key! =Null) {Resultkey =Key } }Else{Resultkey =Objecttomap (key); }} Object value =Entry.getvalue (); Object Resultvalue =Null;if (valueinstanceofCollection) {Resultvalue =Arraytomap ((Collection) value); }Elseif (valueinstanceofMAP) {Resultvalue =Maptomap ((Map) value); }Else{if (valueinstanceofString | | ValueinstanceofBoolean | | ValueinstanceofByte | | ValueinstanceofShort | | ValueinstanceofInteger | | ValueinstanceofLong | | ValueinstanceofFloat | | ValueinstanceofDouble | | ValueinstanceofEnum) {if (Value! =Null) {Resultvalue =Value } }Else{Resultvalue =Objecttomap (value); }} resultmap.put (Resultkey, Resultvalue); }ReturnResultmap; }PrivateStaticCollection Arraytomap (Collection lstobj) {ArrayList ArrayList =NewArrayList ();For(Object t:lstobj) {if (tinstanceofCollection) {Collection result =Arraytomap ((Collection) t); Arraylist.add (result); }Elseif (tinstanceofMAP) {Map result =Maptomap ((MAP) t); Arraylist.add (result); }Else{if (tinstanceofString | | TinstanceofBoolean | | TinstanceofByte | | Tinstanceof short | | t  Integer | | t instanceof Long | | T instanceof Float | | t  Double | | t instanceof Enum) {if (t! = nullelse{Object result =return ArrayList;}}     

JavaBean recursive Map implementation

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.