Convert the map object in the List set to List & lt; object & gt; format instance code, maplist

Source: Internet
Author: User

Convert the map object in the List set to the instance code in the form of List <Object>, maplist

In this example, the map object in the List set is converted to the List <Object> form. The complete code is as follows:

Import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map; import org. apache. commons. beanutils. convertUtils; import org. apache. commons. beanutils. propertyUtils; public class EntityBean {/*** this method implements the automatic * encapsulation function * of data returned by the Map set returned by JDBCTemplate *. The List set stores a series of MAP * objects, obj is a javaBean * @ param listMap Set * @ param objjavaBean object * @ return */public List par Se (List list, Class obj) {// generate ArrayList ary = new ArrayList (); // traverse all data in the set for (int I = 0; I <list. size (); I ++) {try {// generate the Object calendar and encapsulate all parameters in the MAP into the Object. Object o = this. addProperty (Map) list. get (I), obj. newInstance (); // Add the object to the Collection ary. add (o);} catch (InstantiationException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke. printStack Trace () ;}/// return the encapsulated set return list;}/** the value in the Map object is name = aaa, value = bbb call method addProperty (map, user ); * automatically assigns values in the map to the user class * this method is used in combination with the jdbcTemplete of the Spring framework to store the names and values set in * @ param map * @ param obj to be encapsulated object * @ return encapsulated Object */public Object addProperty (Map map Map, object obj) {// traverse all names Iterator it = map. keySet (). iterator (); while (it. hasNext () {// obtain the name String name = it. next (). toString (); // obtain the value String value = map. get (name ). toStrin G (); try {// The Class type that gets the value = PropertyUtils. getPropertyType (obj, name); if (type! = Null) {// set the parameter PropertyUtils. setProperty (obj, name, ConvertUtils. convert (value, type) ;}} catch (Exception ex) {ex. printStackTrace () ;}} return obj ;}// usage method List stuGroupList2 = new ArrayList (); EntityBean entbean = new EntityBean (); for (DynaBean stubean: stuGroupList) {if (stubean. get ("GROUP_ID ")! = Null & stubean. get ("GROUP_ID "). equals (group_id) {LinkedHashMap map = new LinkedHashMap (); map. put ("choose_id", stubean. get ("CHOOSE_ID"); map. put ("group_user_typecode", stubean. get ("GROUP_USER_TYPECODE"); map. put ("group_id", stubean. get ("GROUP_ID"); map. put ("realname", stubean. get ("REALNAME"); stuGroupList2.add (map) ;}} stuGroupList2 = entbean. parse (stuGroupList2, stuGroup. class );

Summary

The above is all about converting the map object in the List set into the List <Object> form instance code. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.