没有警告的
public static void main(String[] args) {
ArrayList<String> vos = new ArrayList<>();
vos.add("123");
List<Object> objects = listClassCast(vos, Object.class);
List<Integer> integers = listClassCast(vos, Integer.class);
}
private static <T> List<T> listClassCast (List<?> source, Class<T> targetClass){
ArrayList<T> ts = new ArrayList<>();
for (Object o : source) {
T t = null;
try {
t = targetClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
assert t != null;
BeanUtils.copyProperties(o,t);
ts.add(t);
}
return ts;
}