Map and object Conversion Based on reflect

Source: Internet
Author: User

/**
* Change the map structure to list and change the list to map structure

* An object is an arbitrary object.
* @ Author liutianfang
* @ Version 2011-8-16 01:16:11 pm
*/
Public class mapobjectutil {
/**
* Change the data structure of the object type to the data structure of the map type.
* @ Param object
* @ Return Map
*/
Public static Map <object, Object> objecttomap (Object O ){
Map <object, Object> map = new hashmap <object, Object> ();
Class A = O. getclass ();
Field [] FS = A. getdeclaredfields ();
For (INT I = 0; I <fs. length; I ++ ){
Map. Put (FS [I]. getname (), reflectutil. getfieldvalue (O, FS [I]. getname (), object. Class ));
}
Return map;
}
/**
* Change the map-type data structure to the object-type data structure.
* @ Param Map <object, Object>
* @ Return object
*/
Public static object maptoobject (Map <object, Object> M, object ob ){
For (iterator = M. entryset (). iterator (); iterator. hasnext ();){
Entry o = (entry) iterator. Next ();
Reflectutil. setfieldvalue (OB, (string) O. getkey (), object. Class, O. getvalue ());
}
Return ob;
}
 
/**
* Change the map-type data structure to the object-type data structure (case-insensitive)
* @ Param Map <object, Object>
* @ Return object
*/
Public static object maptoobjectdefault (Map <object, Object> M, object ob ){
For (iterator = M. entryset (). iterator (); iterator. hasnext ();){
Entry o = (entry) iterator. Next ();
Reflectutil. setfieldvaluedefault (OB, (string) O. getkey (), object. Class, O. getvalue ());
}
Return ob;
}
}

// End

The referenced reflectutil Class address:

Http://blog.csdn.net/s445320/article/details/8929415

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.