Converts the map object in the List collection into Listless than object greater than form instance code
- 2020-12-19 21:06:21
- OfStack
The main implementation of this article is to convert map objects in the List collection to 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 implementation JDBCTemplate
* The returned Map Automatic collection of data
* Encapsulated functionality
* List Collection store 1 A series of MAP
* Object, obj for 1 a javaBean
* @param listMap A collection of
* @param objjavaBean object
* @return
*/
public List parse(List list,Class obj){
// Generating set
ArrayList ary = new ArrayList();
// Iterate over all the data in the collection
for (int i = 0; i<list.size(); i++){
try {
//// Generate object real history will MAP All parameters are encapsulated in the object
Object o = this.addProperty( (Map)list.get(i),obj.newInstance() );
// Adds an object to a collection
ary.add(o);
}
catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Returns the encapsulated collection
return list;
}
/**Map The value in object is name=aaa,value=bbb
A method is called
addProperty(map,user);
* Will automatically map Value assigned to user class
* This method combines Spring Framework of the jdbcTemplete Will not
* Often use
* @param map Stores a collection of names and values
* @param obj Object to encapsulate
* @return Encapsulated object
*/
public Object addProperty(Map map,Object obj){
// Iterate over all the names
Iterator it = map.keySet().iterator();
while(it.hasNext()){
// Get the name
String name = it.next().toString();
// Obtains the value
String value = map.get(name).toString();
try{
// Gets the class shape of the value
Class type = PropertyUtils.getPropertyType(obj, name);
if(type!=null){
// Set the parameters
PropertyUtils.setProperty(obj, name,ConvertUtils.convert(value, type));
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
return obj;
}
}
// Method of use
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);
conclusion
That's all about converting map objects in the List collection to List < object > The full content of the form instance code, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!