The example in this article mainly implements converting map objects in List collection into List<Object> form. The following is the complete code:
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 the returned Map collection* The List collection stores a series of MAP * objects, obj is a javaBean * @param listMap collection* @param objjavaBean object* @return */public List parse(List list,Class obj){//Generate collection ArrayList ary = new ArrayList();//Transfer all data in the collection for (int i = 0; i<list.size(); i++){try {/// Generate object history 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.printStackTrace();}}//Return the encapsulated collection return list;}/**The value in the Map object is name=aaa,value=bbb Call the method addProperty(map,user); *The value in the map will be automatically assigned to the user class* This method combined with the jdbcTemplete of the Spring framework will be very *very useful* @param map stores the name and value collection* @param obj object to be encapsulated* @return encapsulated object*/public Object addProperty(Map map,Object obj){//Travel through all names Iterator it = map.keySet().iterator(); while(it.hasNext()){//Get the name String name = it.next().toString();//Get the value String value = map.get(name).toString();try{//Get the type of the value Class type = 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);Summarize
The above is all the content of this article about converting map objects in List collections into instance code in List<Object> form. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!