JAVA 实体类List<Entity >转 List<Map>

 例

  public class Student{

    private Integer sex;

    private String name;  

  }

  转:

  [{name="XXX",sex=0}]

 

public static <T extends IdEntity> List<Map<Object,Object>> EntityConvertMap(List<T> list){
List<Map<Object,Object>> l = new LinkedList<>();
try {
for(T t : list){
Map<Object,Object> map = new HashMap<>();
Method[] methods = t.getClass().getMethods();
for (Method method : methods) {
if (method.getName().startsWith("get")) {
String name = method.getName().substring(3);
name = name.substring(0, 1).toLowerCase() + name.substring(1);
Object value = method.invoke(t);
if(method.getGenericReturnType().toString().equals("class java.util.Date")&&value!=null){
value = getNow((Date)value, "yyyy-MM-dd HH:mm:ss");
}
map.put(name,value);
}
}
l.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
return l;
}
posted @ 2018-09-07 10:55  叶落の无痕  阅读(12637)  评论(0编辑  收藏  举报