转换a的最佳方法List<POJO> to a List<Map<K,V>>.
是什么?是否有自定义方法/ API?
K = POJO的字段名称,V是对应的值
public class POJO implements Serializable{
String name;
String age;
//getters and setters
}
Run Code Online (Sandbox Code Playgroud)
对于好的和旧的Introspector来说,这听起来像是一份工作.
工作范例:
// Don't be lazy like this, do something about the exceptions
public static void main(String[] args) throws Exception {
List<POJO> pojos = new ArrayList<POJO>();
POJO p1 = new POJO();
p1.setAge("20");
p1.setName("Name");
pojos.add(p1);
POJO p2 = new POJO();
// ...
System.out.println(convertCollection(pojos));
}
public static List<Map<String, ?>> convertCollection(Collection collection)
throws Exception {
List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
for (Object element : collection) {
list.add(getValues(element));
}
return list;
}
public static Map<String, ?> getValues(Object o)
throws Exception {
Map<String, Object> values = new HashMap<String, Object>();
BeanInfo info = Introspector.getBeanInfo(o.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
// This will access public properties through getters
Method getter = pd.getReadMethod();
if (getter != null)
values.put(pd.getName(), getter.invoke(o));
}
return values;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14488 次 |
| 最近记录: |