1、Object转实体对象list
/**
* Object 转 List方法
*
* @param obj 原Object对象
* @param clazz 要转换的对应实体类
* @param <T>
* @return 实体对象集合
*/
private static <T> List<User> objToList(Object obj, Class<T> clazz) {
List userList = new ArrayList<>();
if (obj instanceof List<?>) {
for (Object o : (List<?>) obj) {
userList.add(clazz.cast(o));
}
}
return userList;
}
测试
@Test
public void testTypeConvert() {
User u1 = new User("001", "smID", 0);
User u2 = new User("002", "SMID", 0);
List<User> users = new ArrayList<>();
users.add(u1);
users.add(u2);
Map<String, Object> map = new HashMap<>();
map.put("data", users);
Object data = map.get("data");
List<User> list = objToList(data, User.class);
System.out.println(list);
}
输出:
[TestTime.User(id=001, name=smID, status=0), TestTime.User(id=002, name=SMID, status=0)]