实体类、对象转成map的工具方法
程序员文章站
2022-06-15 09:50:40
...
/**
* 实体类转Map
* @param object
* @return
*/
public static Map<String,Object> entityToMap(Object object) {
Map<String, Object> map = new HashMap();
for (Field field : object.getClass().getDeclaredFields()){
try {
boolean flag = field.isAccessible();
field.setAccessible(true);
Object o = field.get(object);
map.put(field.getName(),o);
field.setAccessible(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(map.entrySet());
Collections.sort(infoIds,new Comparator<Map.Entry<String,Object>>() {
@Override
public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
return o1.getKey().compareTo(o2.getKey());
}
});
return map;
}
上一篇: Map转成JSON对象
下一篇: Opencv 稀疏光流法跟踪详细教程