javabean map list?转listmap

package a;


import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class BeanMapUtils {
private static Log logger = LogFactory.getLog(BeanMapUtils.class);


public BeanMapUtils() {
}


public static Object getMap2Bean(Map map, Class<?> beanClass) {
Object inst = null;
try {
Field[] fields = beanClass.getDeclaredFields();


Constructor<?> constructor1 = beanClass.getConstructor(new Class[0]);
inst = constructor1.newInstance(new Object[0]);


for (Field field : fields) {
if (!field.getName().equals("serialVersionUID")) {
try {

PropertyDescriptor pd = new PropertyDescriptor(field.getName(), inst.getClass());
Method method = pd.getWriteMethod();
if (map.get(field.getName()) != null) {
if (("Long".equals(field.getType().getSimpleName()))
|| ("long".equals(field.getType().getSimpleName()))) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: Long.valueOf(Long.parseLong(map.get(field.getName()).toString())) });
} else if ("Date".equals(field.getType().getSimpleName())) {
if ("java.util.Date".equals(field.getType())) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString())
? null
: DateUtil.formatDate(map.get(field.getName()).toString()) });
} else {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString())
? null
: DateUtil.formatDate(map.get(field.getName()).toString()) });
}
} else if (("Integer".equals(field.getType().getSimpleName()))
|| ("int".equals(field.getType().getSimpleName()))) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: Integer.valueOf(
Integer.parseInt(map.get(field.getName()).toString())) });
} else if ("Double".equals(field.getType().getSimpleName())) {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: Double.valueOf(
Double.parseDouble(map.get(field.getName()).toString())) });
} else if ("List".equals(field.getType().getSimpleName())) {
method.invoke(inst, new Object[] {
map.get(field.getName()) == null ? null : (List) map.get(field.getName()) });
} else if ("Map".equals(field.getType().getSimpleName())) {
method.invoke(inst, new Object[] {
map.get(field.getName()) == null ? null : (Map) map.get(field.getName()) });
} else if ("String".equals(field.getType().getSimpleName())) {
if (map.get(field.getName()).equals("")) {
method.invoke(inst, new Object[] { "" });
} else {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString())
? null : map.get(field.getName()) });
}
} else {
method.invoke(inst,
new Object[] { StringUtils.isEmpty(map.get(field.getName()).toString()) ? null
: map.get(field.getName()) });
}
}
} catch (Exception e) {
logger.error(e);
}
}
}
} catch (Exception e) {
logger.error(e);
}
return inst;
}


public static Map getBean2Map(Object beanObj) {
Map map = new HashMap();
if (beanObj == null)/* 80 */ return map;
try {
Class<?> classType = beanObj.getClass();
Field[] fields = classType.getDeclaredFields();


for (Field field : fields) {
if (!field.getName().equals("serialVersionUID")) {
try {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
Method method = pd.getReadMethod();
Object value = method.invoke(beanObj, new Object[0]);
if (value != null) {
map.put(field.getName(), value);
} else if (("Long".equals(field.getType().getSimpleName()))
|| ("Integer".equals(field.getType().getSimpleName()))) {
map.put(field.getName(), Integer.valueOf(-1));
} else {
map.put(field.getName(), "");
}
} catch (Exception e) {
logger.error(e);
}
}
}
} catch (Exception e) {
logger.error(e);
}
return map;
}

}





package a;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateUtil {


public DateUtil() {
}


public static Date formatDate(String date) {
SimpleDateFormat simple = null;
switch (date.trim().length()) {
case 19:
simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
break;
case 10:
simple = new SimpleDateFormat("yyyy-MM-dd");
break;
case 8:
simple = new SimpleDateFormat("HH:mm:ss");
break;
}


try {
return simple.parse(date.trim());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}


}





package a;


import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;






public class HHSYBeanMapUtils extends BeanMapUtils {


 private static Log logger = LogFactory.getLog(HHSYBeanMapUtils.class);

 
 /**
  * 
  * 有多次继承属性的javaBean转Map;
  * 
  * @param beanObj
  * @return
  */
 
   @SuppressWarnings({ "rawtypes", "unchecked" })
public static Map HHSYgetBean2Map(Object beanObj)
   {
     Map map = new HashMap();
     if (beanObj == null)
       return map;
     try {
       Class<?> classType = beanObj.getClass();
       
       
       Field[] fields = classType.getDeclaredFields();
       do{
       for (Field field : fields) {
         if (!field.getName().equals("serialVersionUID")){
           try{
             PropertyDescriptor pd = new PropertyDescriptor(field.getName(), beanObj.getClass());
             Method method = pd.getReadMethod();
             Object value = method.invoke(beanObj, new Object[0]);
             if (value != null) {
               map.put(field.getName(), value);
             }else if (("Long".equals(field.getType().getSimpleName())) || ("Integer".equals(field.getType().getSimpleName()))) {
               map.put(field.getName(), Integer.valueOf(-1));
            } else {
               map.put(field.getName(), "");
            }
           }catch (Exception e) {
             logger.error(e);
           } 
           }
       }
       classType  = classType.getSuperclass();  
        fields  = classType.getDeclaredFields();
       }while(!classType.getName().equals("java.lang.Object"));//最高父类Object
     } catch (Exception e) {
       logger.error(e);
     }
     return map;
   }
   
   
   /**
    * 
    * List<?>转List<Map<Object,Object>>
    * @param beanList
    * @return
    * @throws Exception
    */
   
   
   public static List<Map<Object,Object>> convertListBean2ListMap(List<?> beanList) throws Exception {  
        List<Map<Object,Object>> mapList = new ArrayList<Map<Object,Object>>();
        if(beanList!=null && beanList.size()>0){
         for(int i=0, n=beanList.size(); i<n; i++){
            Object bean = beanList.get(i);
            Map map = HHSYgetBean2Map(bean);
            mapList.add(map);
        }
        }
        return mapList;  
    }
 }



可以使用Java 8的Stream API来实现List<Map>List<JavaBean>的操作。首先,假设我们有一个Map的列表List<Map<String, Object>>,其中每个Map表示一个JavaBean对象的属性集合。我们需要将这些Map换为对应的JavaBean列表List<JavaBean>。 首先,需要定义一个JavaBean类,该类包含与Map中的键对应的属性和相应的setter和getter方法。例如: ```java public class JavaBean { private String property1; private int property2; // Getter and Setter methods public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public int getProperty2() { return property2; } public void setProperty2(int property2) { this.property2 = property2; } } ``` 然后,我们可以使用Stream API来执行换操作。具体步骤如下: ```java List<Map<String, Object>> mapList = ...; // 假设这是我们要换的List<Map> List<JavaBean> beanList = mapList.stream() .map(map -> { JavaBean bean = new JavaBean(); bean.setProperty1((String) map.get("property1")); bean.setProperty2((int) map.get("property2")); return bean; }) .collect(Collectors.toList()); ``` 在上述代码中,我们使用stream()方法将mapList换为一个流,然后使用map()方法将每个Map换为对应的JavaBean对象。最后,使用collect()方法将流换为List<JavaBean>。 需要注意的是,这里假设每个Map中的键与JavaBean的属性名相对应,并且属性的类型匹配。如果存在不匹配的情况,需要进行适当的类型换和异常处理。 希望对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值