JAVA 实体类List转 List

 例

  public class Student{

    private Integer sex;

    private String name;  

  }

  转:

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

 

public static  List> EntityConvertMap(List list){
List> l = new LinkedList<>();
try {
for(T t : list){
Map 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;
}

转载于:https://www.cnblogs.com/chyk/p/9603272.html

你可能感兴趣的:(JAVA 实体类List转 List)